delete generates faulty sql for subclasses of non-abstract parents
|Reported by:||Daniel Bergey||Owned by:||Daniel Bergey|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||yes||Patch needs improvement:||no|
Stack trace is attached.
Student is a subclass of Person. Person has a ForeignKey field pointing to Address. delete() calls the sql
UPDATE "tables_student" SET "address_id" = NULL WHERE "person_ptr_id" IN
Since address_id is a field of tables_person, this throws an exception.
Assuming CollectedObjects() is doing the right thing, the (a?) correct behavior would be to pass over address_id in line 836 of db/models/query.py (see below) when it shows up as a field of Student. After deleting the Student record, delete_objects will reach the related Person record, which has the real address_id field, set that to Null, and then delete the Address record.
The code I'm proposing changing:
831 del_query.delete_batch_related(pk_list) 832 833 update_query = sql.UpdateQuery(cls, connection) 834 for field in cls._meta.fields: 835 if field.rel and field.null and field.rel.to in seen_objs: 836 -> update_query.clear_related(field, pk_list)
I'm interested in testing the boundaries of this bug and writing a patch. I'm hoping someone can tell me if there's an easy way to test (line 835) whether field is local to cls or inherited from a parent.