Opened 7 years ago

Closed 7 years ago

Last modified 4 years ago

#7853 closed (fixed)

Problem with model inheritance when parent has relation to self

Reported by: osanha@… Owned by: mtredinnick
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 russellm)

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

Download all attachments as: .zip

Change History (10)

comment:1 Changed 7 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 7 years ago by russellm

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

Duplicate of #6755.

comment:3 Changed 7 years ago by Sanha

  • Component changed from django-admin.py to Database wrapper
  • Keywords django admin removed
  • Resolution duplicate deleted
  • Status changed from closed to reopened

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 follow-up: Changed 7 years ago by charmless

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

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 7 years ago by russellm

  • Description modified (diff)
  • milestone set to 1.0 beta
  • Resolution duplicate deleted
  • Status changed from closed to reopened
  • Summary changed from django admin problem used model inheritance to Problem 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 7 years ago by russellm

Test case demonstrating problem

comment:6 Changed 7 years ago by russellm

  • Triage Stage changed from Unreviewed to Accepted

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 7 years ago by mtredinnick

  • Owner changed from nobody to mtredinnick
  • Status changed from reopened to new

comment:8 Changed 7 years ago by mtredinnick

  • Resolution set to fixed
  • Status changed from new to closed

Fixed in [8128].

comment:9 Changed 4 years ago by jacob

  • milestone 1.0 beta deleted

Milestone 1.0 beta deleted

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