[sqlalchemy] Re: Bug (?) in combined 1:n:1 + m:n relation mapping
On Oct 16, 7:42 pm, Michael Bayer [EMAIL PROTECTED] wrote: On Oct 16, 2007, at 1:30 PM, [EMAIL PROTECTED] wrote: Am I really supposed to save/flush/clear/query my objects to get the correct mappings? Or is this a bug? the advised pattern here is the association object pattern described in the docs: http://www.sqlalchemy.org/docs/04/ mappers.html#advdatamapping_relation_patterns_association Ah, great. Thank you for your advice! Using the association proxy http://www.sqlalchemy.org/docs/04/plugins.html#plugins_associationproxy I can also easily access the entities without explicitly using the association: u1 = User(test user 1) e1 = Event(test event 1) u1.events.append(e1) # association automatically created u2 = User(test user 2) e2 = Event(test event 2) p2 = Participation(user=u2, event=e2, status=42) # explicit creation of the association Modified code for classes and mappers: from sqlalchemy.ext.associationproxy import association_proxy class User(object): def __init__(self, name=None): self.name = name events = association_proxy('participations', 'event', creator=lambda e: Participation(event=e)) class Event(object): def __init__(self, title=None): self.title = title users = association_proxy('participations', 'user', creator=lambda u: Participation(user=u)) class Participation(object): def __init__(self, event=None, user=None, status=0): self.event = event self.user = user self.status = status mapper(User, user_table, properties=dict( participations=relation(Participation, backref='user'), ) ) mapper(Participation, participation_table, properties=dict( user=relation(User, backref='participations') ) ) mapper(Event, event_table, properties=dict( participations=relation(Participation, backref='event'), ) ) --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: Bug (?) in combined 1:n:1 + m:n relation mapping
On Oct 17, 12:21 pm, Thomas Wittek [EMAIL PROTECTED] wrote: mapper(Participation, participation_table, properties=dict( user=relation(User, backref='participations') ) ) Should be: mapper(Participation, participation_table) --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: Bug (?) in combined 1:n:1 + m:n relation mapping
On Oct 16, 2007, at 1:30 PM, [EMAIL PROTECTED] wrote: Hi everybody! I've got a schema with a combined 1:n:1 + m:n relation. In detail, I have the entities user and event and the relation participation. That leads to 1:n user-participation, n:1 participation-event, and m:n user-event: [user] 1 -- * [participation] * -- 1 [event] * \_/ * Now the (assumed) bug is that the participation mapping only works correctly after saving, flushing, clearing and retrieving the objects (output only included where relevant): u = User(); u.name = test user e = Event(); e.title = test event u.events.append(e) u.participations [] #empty! e.participations [] #empty! session.save(u); session.save(e); session.flush(); session.clear() u = session.query(User).get(1) e = session.query(Event).get(1) [__main__.Participation object at 0x01035F50] # not empty! e.participations [__main__.Participation object at 0x01035F50] # not empty! I think that the results should be identical before and after save/ flush/clear/query. Is this assumption wrong? Am I really supposed to save/flush/clear/query my objects to get the correct mappings? Or is this a bug? the issue is you are using the participation_table as both a mapped table as well as an association table, which is not recommended; SA performs mapping operations with the table in two totally different ways which are not really compatible with each other (i.e., removing an Event from a User can leave existing Participation objects in your session in an invalid state). the advised pattern here is the association object pattern described in the docs: http:// www.sqlalchemy.org/docs/04/ mappers.html#advdatamapping_relation_patterns_association --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---