[Django] #34614: Natural keys for nullable foreign keys

7 views
Skip to first unread message

Django

unread,
May 31, 2023, 3:40:04 PM5/31/23
to django-...@googlegroups.com
#34614: Natural keys for nullable foreign keys
-----------------------------------------+------------------------
Reporter: martinberoiz | Owner: nobody
Type: Bug | Status: new
Component: Uncategorized | Version: 4.2
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------+------------------------
I found that if a fixture has a model with a nullable foreign key (FK) to
a model with natural keys, loaddata fails to load the first object.
For example, slightly modifying the docs example:


{{{
class PersonManager(models.Manager):
def get_by_natural_key(self, name):
return self.get(name=name)

class Person(models.Model):
name = models.CharField(max_length=100, unique=True)
objects = PersonManager()

def natural_key(self):
return (self.name,)

class Book(models.Model):
name = models.CharField(max_length=100)
author = models.ForeignKey(Person, on_delete=models.CASCADE,
null=True)
}}}

(notice the null=True addition to Book)

Then this fixture will fail when attempting to load with loaddata

{{{
[
{
"model": "myapp.book",
"fields": {
"name": "I Ching",
"author": null,
}
},
]
}}}

Notice that "author": [null] in the fixture does not fix it either, since
it will pass None to the manager's get_by_natural_key. Returning None from
get_by_natural_key doesn't help either.

If this is a feature, and models with natural keys can't ever be
referenced by other models with a _nullable_ FK to them, I think it should
probably be explicit in the documentation.

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

Django

unread,
Jun 7, 2023, 7:28:36 AM6/7/23
to django-...@googlegroups.com
#34614: Natural keys for nullable foreign keys
-------------------------------------+-------------------------------------
Reporter: Martin Beroiz | Owner: nobody
Type: Bug | Status: closed
Component: Core | Version: 4.2
(Serialization) | Resolution:
Severity: Normal | worksforme

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 Mariusz Felisiak):

* status: new => closed
* resolution: => worksforme
* component: Uncategorized => Core (Serialization)


Comment:

Thanks for the ticket, however `"author": null` works for me (checked on
Django 4.2.2). You can try to use one of
[https://code.djangoproject.com/wiki/TicketClosingReasons/UseSupportChannels
support channels] when folks should help you debug.

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

Reply all
Reply to author
Forward
0 new messages