15 messages in com.googlegroups.sqlalchemy[sqlalchemy] Re: problem with backref| From | Sent On | Attachments |
|---|---|---|
| Manlio Perillo | 20 Dec 2006 01:39 | |
| Manlio Perillo | 20 Dec 2006 02:06 | |
| Alan Franzoni | 20 Dec 2006 08:25 | |
| Manlio Perillo | 20 Dec 2006 13:06 | |
| Alan Franzoni | 20 Dec 2006 16:23 | |
| Michael Bayer | 20 Dec 2006 16:49 | |
| Manlio Perillo | 21 Dec 2006 01:56 | |
| Manlio Perillo | 21 Dec 2006 02:00 | |
| Alan Franzoni | 21 Dec 2006 04:48 | |
| Lee McFadden | 21 Dec 2006 05:10 | |
| Michael Bayer | 21 Dec 2006 08:17 | |
| Manlio Perillo | 23 Dec 2006 03:08 | |
| Michael Bayer | 23 Dec 2006 16:32 | |
| Michael Bayer | 23 Dec 2006 18:53 | |
| Manlio Perillo | 24 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()
-- Alan Franzoni <alan...@gmail.com> - Togli .xyz dalla mia email per contattarmi. Remove .xyz from my address in order to contact me. - GPG Key Fingerprint (Key ID = FE068F3E): 5C77 9DC3 BD5B 3A28 E7BC 921A 0255 42AA FE06 8F3E
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to sqla...@googlegroups.com
To unsubscribe from this group, send email to
sqla...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---




