[Django] #31394: Impossible to create with an inverse one-to-one relationship

16 views
Skip to first unread message

Django

unread,
Mar 22, 2020, 12:20:28 PM3/22/20
to django-...@googlegroups.com
#31394: Impossible to create with an inverse one-to-one relationship
-------------------------------------+-------------------------------------
Reporter: scwall | Owner: nobody
Type: Bug | Status: new
Component: Database | Version: 2.2
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
Hello, I don't know if this is a bug or if it's something wanted. If I
create two models .

**Work >>>**

Application One:

----

{{{
class Foo(models.Model):

foo_bar = models.IntegerField()

def save(self, *args, **kwargs):
created = self._state.adding
super(EqoLevel, self).save(*args, **kwargs)
if created:
self.bar_set.create()

}}}

----

Application Two:

----

{{{
class Bar(models.Model):

foo = models.ForeignKey("app1.Foo",
on_delete=CASCADE,primary_key=True)
foo_bar_2 = models.IntegerField()

}}}

**Not work >>>**

Application One:

----

{{{
class Foo(models.Model):

foo_bar = models.IntegerField()

def save(self, *args, **kwargs):
created = self._state.adding
super(EqoLevel, self).save(*args, **kwargs)
if created:
self.bar_set.create()

}}}

----

Application Two:

----

{{{
class Bar(models.Model):

foo = models.OneToOneField("app1.Foo", on_delete=CASCADE)
foo_bar_2 = models.IntegerField()

}}}

But if I use a I can't run a one to one creation with an inverse
relationship and yet during makemigrations it says (HINT:
ForeignKey(unique=True) is usually better served by a OneToOneField.),

Yes but without this function I can't find it. I can also use a signal but
I find it a shame to have to create a signal for the simple creation of a
one to one relationship that foreign key can provide.
Thank's

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

Django

unread,
Mar 22, 2020, 12:24:10 PM3/22/20
to django-...@googlegroups.com
#31394: Impossible to create with an inverse one-to-one relationship
-------------------------------------+-------------------------------------
Reporter: scwall | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 2.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by scwall:

Old description:

New description:

**Work >>>**

Application One:

----

{{{
class Foo(models.Model):

foo_bar = models.IntegerField()

}}}

----

Application Two:

----

{{{
class Bar(models.Model):

}}}

**Not work >>>**

Application One:

----

{{{
class Foo(models.Model):

foo_bar = models.IntegerField()

}}}

----

Application Two:

----

{{{
class Bar(models.Model):

}}}

relationship and yet during makemigrations it says (fields.W342) Setting
unique=True on a ForeignKey has the same effect as using a OneToOneField.


(HINT: ForeignKey(unique=True) is usually better served by a
OneToOneField.),

Yes but without this function I can't find it. I can also use a signal but
I find it a shame to have to create a signal for the simple creation of a
one to one relationship that foreign key can provide.
Thank's

--

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

Django

unread,
Mar 22, 2020, 12:25:14 PM3/22/20
to django-...@googlegroups.com
#31394: Impossible to create with an inverse one-to-one relationship
-------------------------------------+-------------------------------------
Reporter: scwall | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 2.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by scwall:

Old description:

> Hello, I don't know if this is a bug or if it's something wanted. If I

> relationship and yet during makemigrations it says (fields.W342)
> Setting unique=True on a ForeignKey has the same effect as using a

> OneToOneField. (HINT: ForeignKey(unique=True) is usually better served by


> a OneToOneField.),
>
> Yes but without this function I can't find it. I can also use a signal
> but I find it a shame to have to create a signal for the simple creation
> of a one to one relationship that foreign key can provide.
> Thank's

New description:

Hello, I don't know if this is a bug or if it's something wanted. If I
create two models .

**Work >>>**

Application One:

----

{{{
class Foo(models.Model):

foo_bar = models.IntegerField()

def save(self, *args, **kwargs):
created = self._state.adding

super(Foo, self).save(*args, **kwargs)
if created:
self.bar_set.create()

}}}

----

Application Two:

----

{{{
class Bar(models.Model):

}}}

**Not work >>>**

Application One:

----

{{{
class Foo(models.Model):

foo_bar = models.IntegerField()

}}}

----

Application Two:

----

{{{
class Bar(models.Model):

}}}

relationship and yet during makemigrations it says (fields.W342) Setting
unique=True on a ForeignKey has the same effect as using a OneToOneField.

(HINT: ForeignKey(unique=True) is usually better served by a
OneToOneField.),

Yes but without this function I can't find it. I can also use a signal but
I find it a shame to have to create a signal for the simple creation of a
one to one relationship that foreign key can provide.
Thank's

--

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

Django

unread,
Mar 23, 2020, 3:40:28 AM3/23/20
to django-...@googlegroups.com
#31394: Impossible to create with an inverse one-to-one relationship.
-------------------------------------+-------------------------------------
Reporter: Pascal de Sélys | Owner: nobody
Type: Bug | Status: closed

Component: Database layer | Version: 2.2
(models, ORM) |
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage:
| 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: => invalid


Comment:

`W342` is a warning you can always ignore it. You can also create a
`Bar()` manually, e.g.
{{{
if created:
from app2.models import Bar
Bar.objects.create(foo=self)
}}}

Please use one of
[https://code.djangoproject.com/wiki/TicketClosingReasons/UseSupportChannels
support channels] if you have any further questions.

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

Reply all
Reply to author
Forward
0 new messages