Re: [Django] #34907: loaddata crashes on objects with natural keys when don't exist on passed database. (was: OperationalError during loaddata of fixtures with natural keys on multi-db)

0 views
Skip to first unread message

Django

unread,
Oct 20, 2023, 8:29:58 AM10/20/23
to django-...@googlegroups.com
#34907: loaddata crashes on objects with natural keys when don't exist on passed
database.
-------------------------------------+-------------------------------------
Reporter: Florian | Owner: AbId KhAn
Type: Bug | Status: assigned
Component: Core | Version: 4.2
(Serialization) |
Severity: Normal | Resolution:
Keywords: natural key, multi- | Triage Stage: Accepted
db, loaddata |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* component: Database layer (models, ORM) => Core (Serialization)
* stage: Unreviewed => Accepted


Comment:

Thanks for the report.

Django doesn't need to search for an object in the database when you don't
use natural key, that's why it works.

When natural keys are used Django tries to find a matching object in the
database when building an instance based on JSON values. We could probably
avoid this with:
{{{#!diff
diff --git a/django/core/serializers/python.py
b/django/core/serializers/python.py
index 7ec894aa00..47606aa248 100644
--- a/django/core/serializers/python.py
+++ b/django/core/serializers/python.py
@@ -117,6 +117,8 @@ def Deserializer(
continue
else:
raise
+ if not router.allow_migrate_model(self.using, Model):
+ continue
data = {}
if "pk" in d:
try:
}}}

Does it work for you?

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

Reply all
Reply to author
Forward
0 new messages