Opened 6 years ago

Closed 3 years ago

#13822 closed Bug (wontfix)

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

Reported by: Suor Owned by: Suor
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 Suor 6 years ago.

Download all attachments as: .zip

Change History (7)

Changed 6 years ago by Suor

comment:1 Changed 6 years ago by lrekucki

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

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 6 years ago by anonymous

  • Triage Stage changed from Unreviewed to Design 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 5 years ago by graham_king

  • Severity set to Normal
  • Type set to Bug

comment:4 Changed 5 years ago by aaugustin

  • UI/UX unset

Change UI/UX from NULL to False.

comment:5 Changed 5 years ago by aaugustin

  • Easy pickings unset

Change Easy pickings from NULL to False.

comment:6 Changed 3 years ago by aaugustin

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

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