Bulk create not creating objects (UUID PK)
See original GitHub issueI’m using a model with UUID primary key, and if I turn off use_bulk
I get objects created, but with bulk, I don’t get any objects created.
This sounds like #1274
To Reproduce My models;
import uuid
from django.conf import settings
from django.db import models
from django.utils.translation import gettext_lazy as _
class Passport(models.Model):
id = models.UUIDField(
primary_key=True,
default=uuid.uuid4,
editable=False
)
user = models.OneToOneField(
to=settings.AUTH_USER_MODEL,
on_delete=models.CASCADE
)
class Result(models.Model):
""" Race result """
id = models.UUIDField(
primary_key=True,
default=uuid.uuid4,
editable=False
)
passport = models.ForeignKey(
to='results.Passport',
null=True,
blank=True,
on_delete=models.SET_NULL
)
first_name = models.CharField(
verbose_name=_("First name"),
max_length=150
)
last_name = models.CharField(
verbose_name=_("Last name"),
max_length=150
)
finish_time = models.DurationField()
verified = models.BooleanField(
db_index=True,
default=False
)
verified_by = models.ForeignKey(
to=settings.AUTH_USER_MODEL,
blank=True,
null=True,
related_name="verified_results",
on_delete=models.SET_NULL
)
invalid_passport = models.CharField(
max_length=255,
blank=True,
null=True,
help_text=_("An invalid passport number")
)
My resource;
from import_export import fields, resources
from import_export.instance_loaders import CachedInstanceLoader
from results.models import Passport, Result
from results.resources import widgets
class ResultResource(resources.ModelResource):
""" Integrate django-import-export with the Result model """
passport = fields.Field(
column_name='passport',
attribute='passport',
widget=widgets.PassportForeignKeyWidget(Passport, 'id') # this does a filter on id=row['passport']
)
class Meta:
instance_loader_class = CachedInstanceLoader
fields = (
'id',
'first_name',
'last_name',
'finish_time',
'passport',
)
model = Result
skip_html_diff = True
use_bulk = True
def before_save_instance(self, instance, using_transactions, dry_run):
""" Sets the verified flags where there is a passport match. """
if instance.passport:
instance.verified = True
instance.verified_by = self.import_job.author # this data is from django-import-export-celery
CSV data;
id,first_name,last_name,bib_num,finish_time,passport
,test,user,123,02:45:31,ae960b55-c56c-4d43-b96f-d4b494262b1a
,test,user1,321,01:54:37,ce1e9f60-63a9-4f45-8766-c9417178b55c
,test,user2,56,00:34:23,ce1e9f60-63a9-4f45-8766-c9417178a12b
Versions (please complete the following information):
- Django Import Export: 2.7.1
- Python: 3.8
- Django: 3.2.12
Expected behavior
I’d expect the above resource class to bulk create 3 Result
instances. But I only get my 3 instances by setting use_bulk = False
Issue Analytics
- State:
- Created a year ago
- Comments:5 (5 by maintainers)
Top Results From Across the Web
How to get primary keys of objects created using django ...
bulk_create Category.objects.bulk_create(data_list) # Get primary Key id ... The solution is to add unique UUID fields to the models, generate their values ...
Read more >Allow QuerySet.bulk_create() to set the primary key of its objects
I keep thinking that what we *really* want is some form of bulk_save(). This should work even if the models are modified instead...
Read more >SQL Performance Best Practices | CockroachDB Docs
Do not include bulk INSERT statements within an explicit transaction. ... Use UUID to generate unique IDs, Good performance; spreads load well; easy...
Read more >Auto-generated primary keys: UUID, serial or identity column?
This article explores the old question what to use for autogenerated primary keys: UUID, serial or identity column?
Read more >Why does Postgres generate an already used PK value?
PostgreSQL will not try to insert duplicate values on its own, it is you (your application, ORM included) who does. It can be...
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
The release 3.x branch successfully bulk imports.
That’s good to hear - thanks for testing!