On Dec 7, 2007, at 5:21 AM, Martin Pengelly-Phillips wrote:

>
> Hello again Michael,
>
> Have read the documentation you referenced, but am still unsure how to
> now delete a Tag without generating the following error: (Note - using
> Postgres in production)
>
> (IntegrityError) update or delete on "tags" violates foreign key
> constraint "employeesTags_tag_id_fkey" on "employeesTags"
> DETAIL:  Key (id)=(3) is still referenced from table "employeesTags".
> 'DELETE FROM tags WHERE tags.id = %(id)s' {'id': 3}
>
> Without the lazy='dynamic' it works fine (correctly deletes entries
> from employeesTags first).
>
> The delete operation I am performing is:
>
> session.begin()
> entry = session.query(Tag).filter_by(id=3).first()
> try:
>    session.delete(entry)
>    session.commit()
> except Exception, error:
>    print error
>    session.rollback()
> else:
>    print 'Deleted successfully'
>

hey Martin -

I think this is actually a bug in sqlalchemy regarding the "dynamic"  
relation; ive added ticket #895.

Ive recently enhanced regular relations to also not unnecessarily load  
backrefs, that code is in trunk if youd like to try it, and i dont  
think it has this particular issue.

Otherwise, for now, when you load the Tag, you have two (well, three)  
choices:  you can load the colleciton of Employees attached to the  
Tag, and explicitly remove the tag from each employee;  or, you can  
implement "ON DELETE CASCADE" in your database on the foreign key in  
question so that it automatically updates itself; finally, you can,  
within the transaction, issue a "DELETE FROM employees_tags where  
tag_id=3" before you call session.commit(), although this might  
conflict with existing Employee records.

implementing ON DELETE CASCADE is definitely the most "legit" way to  
go here since it lets the database do most of the work.








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