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]



On Jul 19, 2006, at 2:09 PM, Randall Smith wrote:

> Michael Bayer wrote:
>> On Jul 18, 2006, at 5:54 PM, Randall Smith wrote:
>>> SQLError: (IntegrityError) null value in column "project_id"  
>>> violates
>>> not-null constraint
>>>  'UPDATE planreview.documents SET project_id=%(project_id)s WHERE
>>> documents.id = %(documents_id)s' {'project_id': None,   
>>> 'documents_id': 13}
>>>
>>> Shouldn't this be a delete statement?
>>>
>> yup.  you need to post a fully working test case (strongly  
>> preferred:  sqlite, single .py file) since i cannot reproduce this  
>> error in  similar setups.
>> --------------------------------------------------------------------- 
>> ----
>> 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
>
> Did it.  Attached is an example of the problem.
>
> Randall
> """Delete fails after access to a lazy attribute.
>
> """
> from sqlalchemy import *
>
> metadata = DynamicMetaData(name="test")
>
> t1 = Table('t1', metadata,
>     Column('id', Integer, primary_key=True),
>     Column('name', String)
>     )
>
> t2 = Table('t2', metadata,
>     Column('id', Integer, primary_key=True),
>     Column('t1_id', Integer, ForeignKey(t1.c.id)),
>     Column('name', String)
>     )
>
> class T1(object):
>     pass
>
> class T2(object):
>     pass
>
> # If I define the relationship off of T1, no problem.  It's when I  
> define the
> # relationship of of T2 that problems occur.
>
> mapper(T1, t1,
> ##    properties={'t2s':relation(T2, backref='t1', private=True)}
> )
> mapper(T2, t2,
>     properties={'t1':relation(T1, backref='t2s', private=True)}
> )
>
> engine = create_engine("sqlite:///:memory:")
> engine.echo = True
> metadata.connect(engine)
> metadata.create_all()
>
> # Populate tables.
> session = create_session(bind_to=engine)
> for i in range(10):
>     t1obj = T1()
>     t1obj.name = 'test'
>     session.save(t1obj)
>     for i in range(5):
>         t2obj = T2()
>         t2obj.name = 'test'
>         t1obj.t2s.append(t2obj)
> session.flush()
> session.close()
>
> # Delete
> session = create_session(bind_to=engine)
> t2obj = session.query(T2).select()[0]
> print t2obj.t1 # This is what causes the problems.
> session.delete(t2obj)
> session.flush()
> session.close()
> ---------------------------------------------------------------------- 
> ---
> 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
> Sqlalchemy-users@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users


-------------------------------------------------------------------------
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
Sqlalchemy-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users

Reply via email to