Opened 11 years ago

Closed 10 years ago

#1839 closed defect (fixed)

ForeignKey('self') with attribute name like model name breaks AddManipulator().save()

Reported by: mir@… Owned by: Adrian Holovaty
Component: Database layer (models, ORM) Version: master
Severity: normal Keywords:
Cc: django@…, gabor@… Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: yes Patch needs improvement: no
Easy pickings: UI/UX:

Description

I use this model:

class Another(models.Model):
    name = models.CharField(maxlength=20, core=True)
    another = models.ForeignKey('self', null=True, related_name='child_set')

Note that the field name another is like the model name, only lower case.

The following code fails:

Another.AddManipulator().save(dict(id='3', name='Another root category', another=None))

This might be an intended restriction, but then it should be checked by model validation. When it strikes, it's rather surprising because you don't see any connection to the attribute name in the backtrace. And it's probably a common thing to name the self relationship after the table.

This is the exception:

Exception:   File "/home/mir/src/django/trunk/tests/doctest.py", line 1243, in __run
    compileflags, 1) in test.globs
  File "<doctest m2o_recursive[10]>", line 1, in ?
    anotherone = Another.AddManipulator().save(dict(id='3', name='Another root category', another=None))
  File "/home/mir/src/django/active/django/db/models/manipulators.py", line 75, in __init__
    self.fields.extend(f.get_manipulator_fields(self.opts, self, self.change, fol))
  File "/home/mir/src/django/active/django/db/models/related.py", line 112, in get_manipulator_fields
    if follow.get(f.name, False):
AttributeError: 'bool' object has no attribute 'get'

db.models.related.RelatedObject.get_manipulator_fields expects a dictionary in follow. But db.models.maniplators.init() sends True.

Attachments (2)

models.py (1.4 KB) - added by mir@… 11 years ago.
unit test demonstrating this problem
m2o_recursive.diff (837 bytes) - added by mir@… 11 years ago.
... or as a patch to existing m2o_recursive test, if you prefer

Download all attachments as: .zip

Change History (17)

Changed 11 years ago by mir@…

Attachment: models.py added

unit test demonstrating this problem

Changed 11 years ago by mir@…

Attachment: m2o_recursive.diff added

... or as a patch to existing m2o_recursive test, if you prefer

comment:1 Changed 11 years ago by Stefano J. Attardi

Could have searched... #1826.

comment:2 Changed 11 years ago by mir@…

Sorry. I do search, but sometime I overlook things.

Anyway, ticket #1826 demonstrates that *any* field that has the same name as the attribute is sufficient to trigger this bug when there's a ForeignKey('self') *anywhere* in the model. It needn't be the ForeignKey field itself.

comment:3 Changed 11 years ago by Stefano J. Attardi <django@…>

Cc: django@… added

comment:4 Changed 10 years ago by mir@…

Note: This is very similar to #2216 (Filtering by a field having same name as the model results in AttributeError)

comment:5 Changed 10 years ago by Russell Keith-Magee

Doesn't appear to be the same problem as #2216. There is no clash in query terms/accessors - the problem seems to be in the construction of the follow term for related fields.

comment:6 Changed 10 years ago by anonymous

Owner: changed from Adrian Holovaty to Russell Keith-Magee

comment:7 Changed 10 years ago by anonymous

Cc: gabor@… added

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

Owner: changed from Russell Keith-Magee to Adrian Holovaty

This should be cleaned up as part of the manipulator refactoring effort.

comment:9 Changed 10 years ago by anonymous

still no fix for this? ouch! got the same problem here...

comment:10 Changed 10 years ago by Gary Wilson <gary.wilson@…>

Triage Stage: UnreviewedAccepted

comment:11 Changed 10 years ago by Gary Wilson <gary.wilson@…>

although manipulators will be going away when newforms is complete, so this might not be worth fixing.

comment:12 Changed 10 years ago by James Bennett

newforms-admin will indeed fix this; the problem is with the follow dict for manipulators.

comment:13 Changed 10 years ago by Gary Wilson <gary.wilson@…>

Needs tests: set

Even if this doesn't get fixed in manipulators, we should make certain it works with newforms with some tests.

comment:14 Changed 10 years ago by James Bennett

(In [4673]) 0.91-bugfixes: Fixed #999 by resolving name clash in the metasystem which could confuse manipulators about which fields they should follow. Refs #1808, #1826, #1839 and #2415, which are variations of this that persist in trunk.

comment:15 Changed 10 years ago by Malcolm Tredinnick

Resolution: fixed
Status: newclosed

(In [4676]) Fixed #1839, #2415, #2536 -- Fixed a generated name clash that was common in
self-referential and circular relations. A lot of community debugging went into
this fix, so thanks to bmurdock@…, Marek Kubica, ramiro, Michael
Radziej (the last two giving test cases showing the problem) and James Bennett
(who did the hard work to actually diagnose the true problem and fix it).

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