Code

Opened 6 years ago

Closed 6 years ago

#8332 closed (duplicate)

delete generates faulty sql for subclasses of non-abstract parents

Reported by: bergey Owned by: bergey
Component: Uncategorized Version: master
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: yes Patch needs improvement: no
Easy pickings: UI/UX:

Description

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.

Attachments (1)

trace (993 bytes) - added by bergey 6 years ago.
stack trace of error

Download all attachments as: .zip

Change History (3)

Changed 6 years ago by bergey

stack trace of error

comment:1 Changed 6 years ago by bergey

  • Needs documentation unset
  • Needs tests set
  • Owner changed from nobody to bergey
  • Patch needs improvement unset
  • Status changed from new to assigned

Found cls._meta.local_fields, which seems like just what I want in line 834. Probably won't have a chance to test before Monday.

(probably should have said in the original report that I'm using gentoo's SVN package; I'm not sure what revision it was built off of.)

comment:2 Changed 6 years ago by bthomas

  • Resolution set to duplicate
  • Status changed from assigned to closed

Looks like a duplicate of #7778

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.