because of the circular reference between two rows, you have to use the
post_update flag on one of the relations.  complicating that is that
you are using backreferences, which means the backref relationship on
the post_update relation also needs to be explicitly spelled out (i
might want to change that, actually, so that backrefs inherit the
post_update flag of their parent).

for clarity, i have named the attributes "<name>_<scalar|collection>"
to differentiate between the scalar and collection version of each
attribute.   you can add the "uselist" flag to make the collections
scalar.

from sqlalchemy import *
db = create_engine( 'sqlite:///:memory:')
meta = BoundMetaData( db)
meta.engine.echo = False

#==================== tables

t1 = Table('table1', meta,
        Column('name', Integer, ),
        Column('id', Integer, primary_key=True),
        Column('t2_id', Integer,
                    ForeignKey('table2.id',
                        use_alter=True,
                        name='zt1id_fk'
                    )
            )
    )

t2 = Table('table2', meta,
       Column('data', Integer, ),
       Column('id', Integer, primary_key=True),
       Column('t1_id', Integer,
                    ForeignKey('table1.id',
                        use_alter=True,
                        name='zt2id_fk'
                    )
            )
    )
meta.create_all()

#==================== mapping

class T1( object):
    def __str__( self): return '%s: %s %s %s' %
(self.__class__.__name__, self.id, self.name, self.tt2_scalar.data)
class T2( object):
    def __str__( self): return '%s: %s %s %s' %
(self.__class__.__name__, self.id, self.data, self.tt1_scalar.name)


mapper( T1, t1, properties={
    'tt2_scalar':relation(T2, primaryjoin=t1.c.t2_id==t2.c.id,
backref='tt1_collection')
})

mapper(T2, t2, properties={
    'tt1_scalar':relation(T1, primaryjoin=t2.c.t1_id==t1.c.id,
backref=backref('tt2_collection', primaryjoin=t2.c.t1_id==t1.c.id,
post_update=True), post_update=True)
})

session = create_session()

q = T1()
q.name = 't1name'
p = T2()
p.data = 't2data'

p.tt1_scalar = q
q.tt2_scalar = p
session.save(p)

session.flush()
SEP = 5*'-'
print 'before:', SEP, q, SEP, p
session.clear()
r = session.query( T1).get_by_id( p.id)
s = session.query( T2).get_by_id( q.id)
print 'after:', SEP, r, SEP, s
#eof


--~--~---------~--~----~------------~-------~--~----~
 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
-~----------~----~----~----~------~----~------~--~---

Reply via email to