Opened 14 years ago

Closed 12 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

Description

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 14 years ago.

Download all attachments as: .zip

Change History (7)

by Alexander Schepanovski, 14 years ago

Attachment: foreignkey_attname.diff added

comment:1 by Łukasz Rekucki, 14 years ago

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

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 by Graham King, 14 years ago

Severity: Normal
Type: Bug

comment:4 by Aymeric Augustin, 13 years ago

UI/UX: unset

Change UI/UX from NULL to False.

comment:5 by Aymeric Augustin, 13 years ago

Easy pickings: unset

Change Easy pickings from NULL to False.

comment:6 by Aymeric Augustin, 12 years ago

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