Unable to import models which use uuid primary key
See original GitHub issueDescribe the bug Importing models which use a uuid primary key (or any value that is generated in python, not the database) fails silently and the models do not import.
To Reproduce Steps to reproduce the behavior:
- Create a model with a uuid primary key (
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
) - Create a custom
ModelResource
class to import and setuse_bulk
= True - Import a file containing NEW records
- The import appears to be successful but the records are not imported
Versions (please complete the following information):
- Django Import Export: 2.5.0
- Python 3.9
- Django 3.2
Expected behavior The records should be imported.
Screenshots N/a
Additional context
I believe it’s related to the following code in the save_instance
method of the import_export.resources.Resource
class:
if self._meta.use_bulk:
if instance.pk:
self.update_instances.append(instance)
else:
self.create_instances.append(instance)
Since, when using a UUID, the pk is generated before the object is saved to the database, checking the value of pk
against the instance is not a valid way to confirm whether the object exists or not.
A simple (if somewhat crude) fix would be, I believe:
if self._meta.use_bulk:
creating = (
(hasattr(instance, "_state") and getattr(instance._state, "adding"))
or instance.pk is None
)
if not creating:
self.update_instances.append(instance)
else:
self.create_instances.append(instance)
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
Using a UUID as a primary key in Django models (generic ...
This app seems to be assuming that the primary key for the model being voted on is an integer. The built-in comments app...
Read more >Migration that switches a model to a UUID primary key fails ...
We migrated a model from a AutoField as id (default behaviour) to a UUIDField in the past, using a migration that looked like...
Read more >How to use UUID as Primary key instead of auto ... - YouTube
What is UUID ? UUIDs stands for Universally Unique Identifiers, used to uniquely identify the table records. In this, you will learn how...
Read more >Migrate Primary Keys to UUIDs - Sequelize/Node
This step meant to go into each model and update each primary key and foreign key relationship to use UUIDs. user_id: { -...
Read more >Django Tutorial Part 3: Using models - Learn web development
from django.db import models from django.urls import reverse class ... If no field is specified as the primary key, Django will ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hi @matthewelwell, Yes I had to go back to the original approach due to the realisation that it wouldn’t actually fix your issue! This will be the final settled approach, but we’re likely delaying it to v3 (release date unknown) due to it breaking the API.
Obviously you’ll have to port it to your fork, or use the branch until v3 is released (which I doubt will be soon).
Thanks again for raising this and testing!
Hey @matthewhegarty that sounds like a much better solution than the one I posted above.
I’ve temporarily forked the code and implemented my solution above to get things working. I will test this next week and let you know 👍