question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to import models which use uuid primary key

See original GitHub issue

Describe 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:

  1. Create a model with a uuid primary key (id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False))
  2. Create a custom ModelResource class to import and set use_bulk = True
  3. Import a file containing NEW records
  4. 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:closed
  • Created 2 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
matthewhegartycommented, May 4, 2021

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!

1reaction
matthewelwellcommented, Apr 30, 2021

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 👍

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found