15 messages in com.googlegroups.sqlalchemy[sqlalchemy] Re: problem with backref
FromSent OnAttachments
Manlio Perillo20 Dec 2006 01:39 
Manlio Perillo20 Dec 2006 02:06 
Alan Franzoni20 Dec 2006 08:25 
Manlio Perillo20 Dec 2006 13:06 
Alan Franzoni20 Dec 2006 16:23 
Michael Bayer20 Dec 2006 16:49 
Manlio Perillo21 Dec 2006 01:56 
Manlio Perillo21 Dec 2006 02:00 
Alan Franzoni21 Dec 2006 04:48 
Lee McFadden21 Dec 2006 05:10 
Michael Bayer21 Dec 2006 08:17 
Manlio Perillo23 Dec 2006 03:08 
Michael Bayer23 Dec 2006 16:32 
Michael Bayer23 Dec 2006 18:53 
Manlio Perillo24 Dec 2006 02:27 
Subject:[sqlalchemy] Re: problem with backref
From:Alan Franzoni (alan@gmail.com)
Date:12/20/2006 08:25:14 AM
List:com.googlegroups.sqlalchemy

[italian mode] Come va Manlio? Ci sente anche qui alla fine :-) io per la fine dell'anno dovrei riuscire a finire il progetto su cui sto lavorando e potrò riprendere finalmente il mio lavoro anche con python.it! [/italian mode]

1st: In order to use a transaction with a pre-existent connection, you should bind the session to the connection. This can be done by creating the session this way:

sess = create_session(bind_to=conn)

This is useful if you want to work both with the SQL layer and with the ORM layer of SA. If you just want to use the ORM layer (like you appear to do), forget the 'conn' and 'trans' in your code, and just do

sess = create_session() trans = sess.create_transaction()

and just use trans.rollback() or trans.commit() in your code.

2nd: If I got what you want: you want to be able to remove the 'B' instance from the 'A' instance without removing the 'A' object from the db, right? session.delete() is the way to go, because that's the way you remove objects using the SA ORM. And you must pull out the 'cascade='all'' from the 'a' relation, because if you instruct SA to do this, it will try to remove its related object (the 'A' instance), which is not what you wont.

Look at this and tell me if it produces the result you would expect:

mapper(A, a) mapper( B, b, properties={ 'a': relation( A, backref=backref('b', lazy=False, uselist=False, cascade='all'), uselist=False ) } )

sess = create_session() o = A('1', '2') o.b = B(o.x, o.y, '3')

sess.save(o) sess.flush() sess.expunge(o) sess.clear() del o

o = sess.query(A).get_by(x="1", y="2") print o.b.z sess.delete(o.b) sess.flush() sess.close()

print o.b s = a.select() print s.execute().fetchall()