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 full code can be found below (tested with 0.4.0b6):

#=============================
from datetime import datetime
from sqlalchemy import MetaData, Table, Column, String, DateTime,
Integer, Boolean, Unicode, ForeignKey, and_, or_, create_engine
from sqlalchemy.orm import mapper, relation, sessionmaker

engine = create_engine('sqlite:///:memory:')
metadata = MetaData()

user_table = Table('user', metadata,
    Column('user_id', Integer, primary_key=True),
    Column('name', Unicode(255))
)

event_table = Table('event', metadata,
    Column('event_id', Integer, primary_key=True),
    Column('title', Unicode(255))
)

participation_table = Table('participation', metadata,
    Column('event_id', Integer, ForeignKey('event.event_id',
onupdate="CASCADE", ondelete="CASCADE"), primary_key=True),
    Column('user_id', Integer, ForeignKey('user.user_id',
onupdate="CASCADE", ondelete="CASCADE"), primary_key=True),
    Column('status', Integer, nullable=False, default=0),
)

metadata.create_all(engine)

class User(object):
    pass

class Event(object):
    pass

class Participation(object):
    pass

mapper(User, user_table,
    properties=dict(
        participations=relation(Participation, backref='user'),
    )
)

mapper(Event, event_table,
    properties=dict(
        users=relation(User, secondary=participation_table,
backref='events'),
        participations=relation(Participation, backref='event'),
    )
)

mapper(Participation, participation_table)

Session = sessionmaker(bind=engine)
session = Session()

u = User()
u.name = "test user"
e = Event()
e.title = "test event"
u.events.append(e)

u.events
e.users

u.participations
e.participations

session.save(u)
session.save(e)
session.flush()
session.clear()
u = session.query(User).get(1)
e = session.query(Event).get(1)
u.participations
e.participations


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to