On Oct 30, 2007, at 5:41 AM, Felix Schwarz wrote:

>
> foo = session.query(User).filter_by(name='Foo Bar').one()
> session.save(foo)
>
> for address in foo.addresses:
>      foo.addresses.remove(address)
>      session.delete(address)
> session.delete(foo)
>
> foo = User()
> session.save(foo)
> foo.id = 1
> foo_addr = Address()
> session.save(foo_addr)
> foo_addr.street = "Picadelly Circus"
> foo.addresses.append(foo_addr)
>
> transaction.commit()

specifically its the "foo.id=1" thats causing it to fail.  by setting  
it, you trigger a special rule in SQLAlchemy designed to deal with  
this, called a "row switch", where it converts your DELETE and INSERT  
into a single UPDATE.  apparently the cascade onto the related  
addresses collection is getting confused; ticket 841 is added.   
additionally, your session.save(foo) right below the query is also  
incorrect (though does not affect the test), ive added ticket 840 to  
ensure an exception is raised there.

if you truly need to "row switch", i.e. your second User needs to  
have the same id #1, issue a flush after the deletion of the previous  
user:

for address in foo.addresses:
     foo.addresses.remove(address)
     session.delete(address)
session.flush()

that will remove the previous address rows from the database before  
getting into the newly added user.

also the removal of the addresses there could be automated by just  
placing "cascade='all,delete-orphan'" on the User.addresses relation.



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