Opened 16 years ago

Closed 16 years ago

Last modified 13 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: dev
Severity: Keywords: model inheritance
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

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 16 years ago.
Test case demonstrating problem

Download all attachments as: .zip

Change History (10)

comment:1 by Sanha, 16 years ago

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

comment:2 by Russell Keith-Magee, 16 years ago

Resolution: duplicate
Status: newclosed

Duplicate of #6755.

comment:3 by Sanha, 16 years ago

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 by charmless, 16 years ago

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.

in reply to:  4 comment:5 by Russell Keith-Magee, 16 years ago

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.

by Russell Keith-Magee, 16 years ago

Attachment: 7853-r8016-tests.diff added

Test case demonstrating problem

comment:6 by Russell Keith-Magee, 16 years ago

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 by Malcolm Tredinnick, 16 years ago

Owner: changed from nobody to Malcolm Tredinnick
Status: reopenednew

comment:8 by Malcolm Tredinnick, 16 years ago

Resolution: fixed
Status: newclosed

Fixed in [8128].

comment:9 by Jacob, 13 years ago

milestone: 1.0 beta

Milestone 1.0 beta deleted

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