Atma
Atma

Reputation: 29767

Django bulk_create causes duplicate entry integrity error

I have the following model:

class GeneratedContent(models.Model):
    entity = models.ForeignKey('companies.Entity')
    source_url = models.URLField(max_length=255)
    title = models.CharField(max_length=255, blank=True, null=True)
    desc = models.TextField(blank=True, null=True)
    created_at = models.DateTimeField(auto_now_add = True)
    updated_at = models.DateTimeField(auto_now = True)

    def __str__(self):  
        return self.entity.name +' Content'

I am then processing some urls and then saving a bulk number of these objects like this:

gen_content_list = []
        for e in entities:
            entity_status = get_tweets(e.twitter_handle())

            try:
                stat_url = re.search("(?P<url>https?://[^\s]+)", entity_status).group("url")
                gen_content = GeneratedContent.objects.create(
                    entity=e,
                    desc=entity_status,
                    source_url=stat_url,
                    crawled=False,
                    )
                gen_content_list.append(gen_content)
                self.stdout.write(self.style.SUCCESS(e.name+' status: '+stat_url.encode('ascii','replace')))
            except:
                pass
        if gen_content_list:
            GeneratedContent.objects.bulk_create(gen_content_list)

I get the following error:

django.db.utils.IntegrityError: (1062, "Duplicate entry '19' for key 'PRIMARY'")

What am I doing wrong?

Upvotes: 1

Views: 3097

Answers (2)

Andrey Topoleov
Andrey Topoleov

Reputation: 1919

To prepare your instances for bulk creation you need to write

GeneratedContent(
                entity=e,
                desc=entity_status,
                source_url=stat_url,
                crawled=False,
                )

Insted of

GeneratedContent.objects.create(...)    # that method send data to db immediately

After that you can call bulk_create method.

For example:

for e in entities:
    ... 
    gen_content = GeneratedContent(
                                   entity=e,
                                   desc=entity_status,
                                   source_url=stat_url,
                                   crawled=False,
                                   )
    gen_content_list.append(gen_content)

GeneratedContent.objects.bulk_create(gen_content_list)

Upvotes: 0

Kevin Christopher Henry
Kevin Christopher Henry

Reputation: 48922

The problem is that you're calling create()—which creates the instance in the database—and then trying to do bulk_create(). Instead, create an unsaved model instance:

gen_content = GeneratedContent(...)

Upvotes: 1

Related Questions