Hi,
i saw the example for the above in the:
http://permalink.gmane.org/gmane.comp.python.sqlalchemy.user/3713
but after several trials to map the tables did not succeed.
The example code below demonstrates the problem - I create 2 objects
that are interconnected and sqlalchemy generates insert for each
object. I suppose it must generate 2 inserts and after that one update.
Can somebody help me in properly mapping the tables?

my example code is:

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.t
class T2( object):
    def __str__( self): return '%s: %s %s %s' %
(self.__class__.__name__, self.id, self.data, self.t
mapper( T1, t1).add_property( 'tt2',
    relation( T2, uselist=False, lazy=False,
#           secondary=t2,
#           secondaryjoin=  t1.c.t2_id==t2.c.id,
        primaryjoin=    t2.c.t1_id==t1.c.id,
#            secondaryjoin=  t2.c.t1_id==t1.c.id,
#            primaryjoin=    t1.c.t2_id==t2.c.id,
#            cascade='all',
        backref=backref( 'tt1', cascade='all', use_list=False,)
    ) )
mapper( T2, t2).add_property( 'tt1',
    relation( T1, uselist=False, lazy=False,
        secondary=t1,
        secondaryjoin=  t2.c.t1_id==t1.c.id,
        primaryjoin=    t1.c.t2_id==t2.c.id,
#            secondaryjoin=  t1.c.t2_id==t2.c.id,
#            primaryjoin =   t2.c.t1_id==t1.c.id,
        backref=backref( 'tt2', cascade='all', use_list=False,)
    ) )
#=================== example

session = create_session()

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

p.tt1 = q
q.tt2 = p
session.save(p)

session.flush()
SEP = 5*'-'
print 'before:', SEP, p, SEP, q
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





Thanks in advance
StefanB


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