Opened 10 years ago

Closed 10 years ago

#23338 closed New feature (fixed)

Warn when unique=True is set on ForeignKey

Reported by: Curtis Maloney Owned by: Tim Graham <timograham@…>
Component: Core (System checks) Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Long ago in the dark times before 1.0, there was a bug in Admin that meant using ForeignKey(unique=True) was preferable to using a OneToOneField.

Somehow, people (on IRC) are still doing this -- even new users-- , and I can't figure out why.

Unless I'm missing something, there is no case where a ForeignKey(unique=True) is preferable to a OneToOneField.

Shouldn't we, then, at least add a check/warn to say this?

Change History (13)

comment:1 by Josh Smeaton, 10 years ago

Triage Stage: UnreviewedAccepted

comment:2 by JLinden, 10 years ago

Owner: changed from nobody to JLinden
Status: newassigned

comment:3 by Daniele Procida, 10 years ago

Keywords: afraid-to-commit added

I've marked this ticket as especially suitable for people following the ​Don't be afraid to commit tutorial at the DjangoCon US 2014 sprints.

If you're tackling this ticket, please don't hesitate to ask me for guidance if you'd like any, either at the sprints themselves, or here or on the Django IRC channels, where I can be found as EvilDMP.

comment:5 by Diego Guimarães, 10 years ago

Needs documentation: unset
Needs tests: unset

New pull request with tests and updated documentation at https://github.com/django/django/pull/3262

comment:6 by Diego Guimarães, 10 years ago

The patch and test are basically working, but a design decision should be made how to handle the tests violating the unique check.

I think that would be helpful if someone that knows better the System Checks could take a look. I detailed the problems in the PR https://github.com/django/django/pull/3262

Last edited 10 years ago by Diego Guimarães (previous) (diff)

comment:7 by Tim Graham, 10 years ago

On the PR Carl says,

Is this check worth adding? A unique ForeignKey works just fine and doesn't hurt anyone. If ForeignKey(unique=True) makes more sense to someone, or if they just changed it to be unique and have a bunch of existing accessor code that they don't want to have to change to OneToOneField style, is there really any value in Django complaining at them about it?

I think if we do add this, the warning needs to be carefully worded so as not to imply that there's anything wrong or broken about ForeignKey(unique=True), because there isn't; it's just that they may find the OneToOneField accessor slightly more convenient if they weren't aware of that option. But to me this seems like a coding-style thing that doesn't warrant a check warning.

comment:8 by Carl Meyer, 10 years ago

Thanks Tim, I should have posted that here instead.

comment:9 by Tim Graham, 10 years ago

Easy pickings: unset
Has patch: set
Patch needs improvement: set

comment:10 by JLinden, 10 years ago

Owner: JLinden removed
Status: assignednew

comment:11 by Tim Graham, 10 years ago

Keywords: afraid-to-commit removed
Patch needs improvement: unset

comment:12 by Tim Graham, 10 years ago

Triage Stage: AcceptedReady for checkin

comment:13 by Tim Graham <timograham@…>, 10 years ago

Owner: set to Tim Graham <timograham@…>
Resolution: fixed
Status: newclosed

In f39b0421b406b411c3bcb58e8aa415d885fea505:

Fixed #23338 -- Added warning when unique=True on ForeigKey

Thanks Jonathan Lindén for the initial patch, and Tim Graham
and Gabe Jackson for the suggestions.

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