[Django] #28824: Allow different Foreign Keys to share same underlying DB column

100 views
Skip to first unread message

Django

unread,
Nov 21, 2017, 2:09:24 AM11/21/17
to django-...@googlegroups.com
#28824: Allow different Foreign Keys to share same underlying DB column
-------------------------------------+-------------------------------------
Reporter: klass- | Owner: nobody
ivan |
Type: New | Status: new
feature |
Component: Database | Version: 1.11
layer (models, ORM) | Keywords: foreigh key,
Severity: Normal | db_column,
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
{{{
class A(Model):
uid = CharField(unique=True)
# other fields

class B(Model):
uid = CharField(unique=True)
# other fields

class C(Model):
# other fields
uid = CharField(unique=False)
a = ForeignKey(to=A, to_field='uid', db_column='uid', null=True)
b = ForeignKey(to=B, to_field='uid', db_column='uid', null=True)
}}}

Gives an error:

{{{
Field 'b' has column name 'uid' that is used by another field.
HINT: Specify a 'db_column' for the field.
}}}

However, ForeignKey field is a ORM relation based on underlying column
anyway.
I think it won't break anything if we allow different foreign keys to
share same underlying column.
This adds more flexibility and will allow better django "on-boarding"
process for legacy projects.
It will be also useful when A or B is proxy model - so we can get
different proxy objects by different foreign keys referencing same DB row.

Another example can be found here:
[https://stackoverflow.com/questions/36911804/django-use-same-column-for-
two-foreign-keys]

--
Ticket URL: <https://code.djangoproject.com/ticket/28824>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Nov 21, 2017, 2:26:33 AM11/21/17
to django-...@googlegroups.com
#28824: Allow different Foreign Keys to share same underlying DB column
-------------------------------------+-------------------------------------
Reporter: klass-ivan | Owner: nobody
Type: New feature | Status: new
Component: Database layer | Version: 1.11
(models, ORM) |
Severity: Normal | Resolution:
Keywords: foreigh key, | Triage Stage:
db_column, | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by klass-ivan):

If skip `_check_column_name_clashes` in models.base, the next issue will
be in `_do_insert` - DB error that "Column '<column_name>' specified
twice"

--
Ticket URL: <https://code.djangoproject.com/ticket/28824#comment:1>

Django

unread,
Nov 21, 2017, 5:50:26 PM11/21/17
to django-...@googlegroups.com
#28824: Allow different Foreign Keys to share same underlying DB column
-------------------------------------+-------------------------------------
Reporter: klass-ivan | Owner: nobody
Type: New feature | Status: new
Component: Database layer | Version: 1.11
(models, ORM) |
Severity: Normal | Resolution:
Keywords: foreigh key, | Triage Stage:
db_column, | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Josh Smeaton):

I'm struggling to see where the value for this change would be, and how
common a situation this really is. I think the benefit of supporting this
would be outweighed by users that are incorrectly duplicating keys on
their models with copy/paste and running into legitimate issues.

Further, I believe you can (probably) get similar behaviour working if you
reverse the location of your keys, and switch to using OneToOne. I haven't
tested this myself, but I think this would work:

{{{
class A(Model):
uid = CharField(unique=True)

c = OneToOneField(to=C, to_field='uid', db_column='uid', null=True)

class B(Model):
uid = CharField(unique=True)

c = OneToOneField(to=C, to_field='uid', db_column='uid', null=True)

class C(Model):
# other fields
uid = CharField(unique=False)

c = get_c(..)
c.a # fine
c.b # fine
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/28824#comment:2>

Django

unread,
Nov 27, 2017, 1:52:35 PM11/27/17
to django-...@googlegroups.com
#28824: Allow different Foreign Keys to share same underlying DB column
-------------------------------------+-------------------------------------
Reporter: klass-ivan | Owner: nobody
Type: New feature | Status: closed

Component: Database layer | Version: 1.11
(models, ORM) |
Severity: Normal | Resolution: wontfix

Keywords: foreigh key, | Triage Stage:
db_column, | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* status: new => closed
* resolution: => wontfix


--
Ticket URL: <https://code.djangoproject.com/ticket/28824#comment:3>

Django

unread,
May 20, 2020, 1:30:43 AM5/20/20
to django-...@googlegroups.com
#28824: Allow different Foreign Keys to share same underlying DB column
-------------------------------------+-------------------------------------
Reporter: klass-ivan | Owner: nobody
Type: New feature | Status: new

Component: Database layer | Version: 1.11
(models, ORM) |
Severity: Normal | Resolution:
Keywords: foreigh key, | Triage Stage:
db_column, | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Matt Ferrante):

* status: closed => new
* resolution: wontfix =>


Comment:

Reopening because I think there is a valid case for this, referenced in my
previous comment.

--
Ticket URL: <https://code.djangoproject.com/ticket/28824#comment:5>

Django

unread,
May 20, 2020, 1:56:18 AM5/20/20
to django-...@googlegroups.com
#28824: Allow different Foreign Keys to share same underlying DB column
-------------------------------------+-------------------------------------
Reporter: klass-ivan | Owner: nobody
Type: New feature | Status: closed

Component: Database layer | Version: 1.11
(models, ORM) |
Severity: Normal | Resolution: wontfix

Keywords: foreigh key, | Triage Stage:
db_column, | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* status: new => closed
* resolution: => wontfix


Comment:

Matt, please
[https://docs.djangoproject.com/en/stable/internals/contributing/triaging-
tickets/#closing-tickets follow triaging guidelines with regards to
wontfix tickets.]

Moreover this comment is still valid, even with a use case:

> I'm struggling to see where the value for this change would be, and how
common a situation this really is. I think the benefit of supporting this
would be outweighed by users that are incorrectly duplicating keys on
their models with copy/paste and running into legitimate issues.

--
Ticket URL: <https://code.djangoproject.com/ticket/28824#comment:6>

Django

unread,
Mar 4, 2021, 10:52:01 PM3/4/21
to django-...@googlegroups.com
#28824: Allow different Foreign Keys to share same underlying DB column
-------------------------------------+-------------------------------------
Reporter: klass-ivan | Owner: nobody
Type: New feature | Status: closed
Component: Database layer | Version: 1.11
(models, ORM) |
Severity: Normal | Resolution: wontfix
Keywords: foreigh key, | Triage Stage:
db_column, | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by James Lin):

This issue becomes more apparent when creating models on top of existing
database just to read from.

--
Ticket URL: <https://code.djangoproject.com/ticket/28824#comment:7>

Reply all
Reply to author
Forward
0 new messages