p = Parent('parent1')
c = Child('child1')
c.parent = p
p.children.append(c) 1) Is it feasible to avoid to use backref or back_populates ? Would it be a bad idea to work without these fonctionnalities ? Will I face for example inconsistent state of the program ?
2) Is it possible to use back_populates and allow SQLAlchemy to detect that it should not append something which was already inserted ?
Sven
class Player():
def __init__(self, name):
self.name = name
self.room = None
def set_room(self, room):
self.room = room
def __repr__(self):
return self.name
class Room():
def __init__(self, name):
self.name = name
self.players = []
def add_player(self, player):
self.players.append(player)
def __repr__(self):
return self.name
room1 = Room("room1")
player1 = Player("player1")
player2 = Player("player2")
# The room is stored in the player instances :
player1.set_room(room1)
player2.set_room(room1)
# And the players are also stored in the room instance :
room1.add_player(player1)
room1.add_player(player2)
print(room1.players)
print(player1.room)
print(player2.room)In this specific example, I could also just delete one of the statement, but :- the two statements are sometimes in different classes and different files. Is it not dirty to decide that one of the statements will be deleted, letting the other do all the work and populate the two attributes ?- it could be a lot of work because for most of relationships, I will have to determine where these statements are, why and when there are used, and think of a cleaner way to organize my code according to SQLAlchemy principles (just one modification start automatically the modification of the linked attribute in the other side). And there is a lot of classes...I think you are right with the fact that it would be maybe too complicated and a bit useless to use SQLAlchemy and still try to keep the model absolutly separated and independant from it."You can go this road for one-to-many and many-to-one although this isnot a well-traveled use case.For a many-to-many with "secondary", there can still be some conflictsbecause the unit of work uses "backref" to look at the fact that thetwo sides represent the same collection to resolve duplicate eventsthat occur on both sides."So, I suppose that it is probably a bad idea to avoid to use backref and back_populates if it is not a well-traveled use case and if there can be some conflicts in many-to-many relations. So, the cleaner way to proceed would be to adapt all the classes and always keep only one modification statement per bidirectional relationship ?
--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper
http://www.sqlalchemy.org/
To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description.
---
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscribe@googlegroups.com.