Opened 8 years ago

Closed 5 years ago

#13822 closed Bug (wontfix)

ForeignKey's attname is counter-intuitive when referencing to a field not named 'id'

Reported by: Alexander Schepanovski Owned by: Alexander Schepanovski
Component: Database layer (models, ORM) Version: 1.2
Severity: Normal Keywords: db, ForeignKey
Cc: Triage Stage: Design decision needed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


class SomeModel(model.Model):
    region = models.ForeignKey(Region, to_field='code')

sm = SomeModel()
sm.region # works
sm.region_code # AttributeError, currently
sm.region_id # works, but evaluates to sm.region.code

Also, currently we have:

CREATE TABLE "app_somemodel" (
    "region_id" integer NOT NULL REFERENCES "geo_region" ("code") DEFERRABLE INITIALLY DEFERRED,

which also looks strange.

Attachments (1)

foreignkey_attname.diff (575 bytes) - added by Alexander Schepanovski 8 years ago.

Download all attachments as: .zip

Change History (7)

Changed 8 years ago by Alexander Schepanovski

Attachment: foreignkey_attname.diff added

comment:1 Changed 8 years ago by Łukasz Rekucki

First, this change is *very* backwards incompatible, as it would require to migrate any db schema that has a foreign key.

Second, I hope that someday Django will support multi-column (primary) keys and it probably won't look so good then.

comment:2 Changed 8 years ago by anonymous

Triage Stage: UnreviewedDesign decision needed

Not *very* backward incompatable since references to fields named "id" (most primary keys) won't trigger changes.
Still it is backward incompatable, promoting to design decision needed.

comment:3 Changed 7 years ago by Graham King

Severity: Normal
Type: Bug

comment:4 Changed 6 years ago by Aymeric Augustin

UI/UX: unset

Change UI/UX from NULL to False.

comment:5 Changed 6 years ago by Aymeric Augustin

Easy pickings: unset

Change Easy pickings from NULL to False.

comment:6 Changed 5 years ago by Aymeric Augustin

Resolution: wontfix
Status: newclosed

I don't think there's much value in this change at this point in the history of Django.

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