Opened 14 years ago

Closed 14 years ago

#12190 closed (fixed)

Changeset [11710] breaks ForeignKey.db_type for fields defined outside a model

Reported by: jittat Owned by: nobody
Component: Database layer (models, ORM) Version: dev
Severity: Keywords: multi-db, ForeignKey
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

When defining a ForeignKey field outside a model, ForeignKey.db_type produces an exception FieldDoesNotExist. This bug breaks south 0.6.2, and starts occurring after changeset [11710].

Steps to reproduce

  1. Assume that you already have a Poll model defined as in the tutorial
  1. In manage.py shell, do the following
from polls.models import Poll
from django.db import models
poll = models.ForeignKey(Poll)
poll.db_type()

You expect to see 'integer' (on revision [11709], you see it), but you'll get the following:

  File "<console>", line 1, in <module>
  File "/usr/lib/python2.6/dist-packages/django/db/models/fields/related.py", line 771, in db_type
    rel_field = self.rel.get_related_field()
  File "/usr/lib/python2.6/dist-packages/django/db/models/fields/related.py", line 648, in get_related_field
    data = self.to._meta.get_field_by_name(self.field_name)
  File "/usr/lib/python2.6/dist-packages/django/db/models/options.py", line 299, in get_field_by_name
    % (self.object_name, name))
FieldDoesNotExist: Poll has no field named None

Additional information

After some tracing, I think this bug is related to the removal of line 693 in source:/django/trunk/django/db/models/fields/related.py#11710 in changeset [11710]. The following line is removed:

            to_field = to_field or to._meta.pk.name 

Change History (2)

comment:1 by jittat, 14 years ago

Sorry, the link to the original file is wrong (it points to the file after the change of [11710]). The correct link is line 693 in source:/django/trunk/django/db/models/fields/related.py#11709#L693.

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

Resolution: fixed
Status: newclosed

(In [11730]) Fixed #12190 -- Corrected a regression in the ability to instantiate ForeignKeys? outside of models. Thanks to jittat for the report.

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