Michael Bayer wrote:
> OK this test program is working correctly.
>
> the mapper setup you are trying is:
>
> mapper(T1, t1)
> mapper(T2, t2,
> properties={'t1':relation(T1, backref='t2s', private=True)}
> )
>
> and then the deletion looks like:
>
> UPDATE t2 SET t1_id=? WHERE t2.id = ?
> [None, 2]
> UPDATE t2 SET t1_id=? WHERE t2.id = ?
> [None, 3]
> UPDATE t2 SET t1_id=? WHERE t2.id = ?
> [None, 4]
> UPDATE t2 SET t1_id=? WHERE t2.id = ?
> [None, 5]
> DELETE FROM t2 WHERE t2.id = ?
> [1]
> DELETE FROM t1 WHERE t1.id = ?
> [1]
>
> the private=True means that when a "t2" is deleted, the child "t1"
> should be deleted. it does not mean that when a "t1" is deleted, all
> of its "t2s" should be deleted as well. so when you delete a "t2",
> its deleting its child "t1", and detaching all of the other "t2"s
> from the "t1" since they are not slated for deletion (it works so
> great ! :) )
>
> so instead, you are looking for the "t2s" relationship to be private
> as well, which you can do like this:
>
> mapper(T1, t1)
> mapper(T2, t2,
> properties={'t1':relation(T1, backref=backref('t2s',
> private=True), private=True)}
> )
>
> so then when you delete a "t2", that triggers the delete of the "t1",
> and then thats private so it deletes all of its "t2"s...so then the
> SQL you get when deleting a t2 is:
>
> DELETE FROM t2 WHERE t2.id = ?
> [[1], [2], [3], [4], [5]]
> DELETE FROM t1 WHERE t1.id = ?
> [1]
>
Wow. What I was going for was:
mapper(T1, t1)
mapper(T2, t2,
properties={'t1':relation(T1, backref=backref('t2s',
private=True))}
)
So I should have defined private in the backref.
It didn't even cross my mind that SA could enforce private from
child->parent. I had assumed it would figure out which one was the
parent and enforce private from parent->child. SA continues to amaze
me. Truly incredible software.
Randall
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Sqlalchemy-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users