Opened 8 years ago

Closed 8 years ago

Last modified 5 years ago

#7853 closed (fixed)

Problem with model inheritance when parent has relation to self

Reported by: osanha@… Owned by: Malcolm Tredinnick
Component: Database layer (models, ORM) Version: master
Severity: Keywords: model inheritance
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description (last modified by Russell Keith-Magee)

Test models:

class Parent(models.Model):
    s = models.ForeignKey('self', null=True, blank=True)
    a = models.TextField()
    
class Child(Parent):
    b = models.TextField()

Test session:

>>> child = Child.objects.create(a='parent field', b='child field')
>>> child.delete()
Traceback (most recent call last):
...
ProgrammingError:  "s_id" column is not exist at "forums_child" relation.

These error was begined at 0.97-pre-SVN-8007
as I know, previous version is not shown these errors.

Attachments (1)

7853-r8016-tests.diff (1.0 KB) - added by Russell Keith-Magee 8 years ago.
Test case demonstrating problem

Download all attachments as: .zip

Change History (10)

comment:1 Changed 8 years ago by Sanha

Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset

Is it related with 'newforms-admin' ?
http://code.djangoproject.com/changeset/7964

comment:2 Changed 8 years ago by Russell Keith-Magee

Resolution: duplicate
Status: newclosed

Duplicate of #6755.

comment:3 Changed 8 years ago by Sanha

Component: django-admin.pyDatabase wrapper
Keywords: django admin removed
Resolution: duplicate
Status: closedreopened

I think that second issue is valid.

when parent model has foreign key to self, delete child (or parent) object was failed because finding parent's foreign key.

see below.

class Parent(models.Model):
    s = models.ForeignKey('self', null=True, blank=True)
# or same:  s = models.ForeignKey('Parent', null=True, blank=True)
    a = models.TextField()
    
class Child(Parent):
    b = models.TextField()

python shell

>>> child = Child.objects.create(a='parent field', b='child field')
>>> child.delete()
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/usr/lib64/python2.5/site-packages/django/db/models/base.py", line 423, in delete
    delete_objects(seen_objs)
  File "/usr/lib64/python2.5/site-packages/django/db/models/query.py", line 836, in delete_objects
    update_query.clear_related(field, pk_list)
  File "/usr/lib64/python2.5/site-packages/django/db/models/sql/subqueries.py", line 211, in clear_related
    self.execute_sql(None)
  File "/usr/lib64/python2.5/site-packages/django/db/models/sql/subqueries.py", line 112, in execute_sql
    super(UpdateQuery, self).execute_sql(result_type)
  File "/usr/lib64/python2.5/site-packages/django/db/models/sql/query.py", line 1608, in execute_sql
    cursor.execute(sql, params)
  File "/usr/lib64/python2.5/site-packages/django/db/backends/util.py", line 18, in execute
    return self.cursor.execute(sql, params)
ProgrammingError:  "s_id" column is not exist at "forums_child" relation.

comment:4 Changed 8 years ago by charmless

Resolution: duplicate
Status: reopenedclosed

Marking a ticket as a Duplicate means the problem is known and being worked on at another ticket. It is not meant to imply that a problem isn't valid (if it were, the duplicate would be closed too :) )

For browsers: Duplicate of #6755.

comment:5 in reply to:  4 Changed 8 years ago by Russell Keith-Magee

Description: modified (diff)
milestone: 1.0 beta
Resolution: duplicate
Status: closedreopened
Summary: django admin problem used model inheritanceProblem with model inheritance when parent has relation to self

Replying to charmless:

Marking a ticket as a Duplicate means the problem is known and being worked on at another ticket. It is not meant to imply that a problem isn't valid (if it were, the duplicate would be closed too :) )

In this case, it looks like the reporter has put two issues in a single ticket (which, by the way, is very bad form). The first issue (inheritance doesn't work in admin) is clearly a duplicate of #6755. The second part of the report isn't obviously a duplicate, so it requires independent verification.

I've updated the ticket title and description to describe the problem better.

Changed 8 years ago by Russell Keith-Magee

Attachment: 7853-r8016-tests.diff added

Test case demonstrating problem

comment:6 Changed 8 years ago by Russell Keith-Magee

Triage Stage: UnreviewedAccepted

The failure occurs when an update call is made to clear the foreign key, but the update call assumes the foreign key is on the child class, not the parent. The action is correct; the SQL that is generate is not. The solution isn't immediately obvious to me, so I've uploaded the test case in case someone else gets to this problem first.

comment:7 Changed 8 years ago by Malcolm Tredinnick

Owner: changed from nobody to Malcolm Tredinnick
Status: reopenednew

comment:8 Changed 8 years ago by Malcolm Tredinnick

Resolution: fixed
Status: newclosed

Fixed in [8128].

comment:9 Changed 5 years ago by Jacob

milestone: 1.0 beta

Milestone 1.0 beta deleted

Note: See TracTickets for help on using tickets.
Back to Top