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.

ValueError: Can't bulk create a multi-table inherited model

See original GitHub issue

Problem

I have a super Notification(models.Model) class which I use to create many notification subclasses such as PostNotification(Notification), CommentNotification(Notification), etc., and when trying to run CommentNotification.objects.bulk_create(my_list_of_notification_objects), i get the following traceback:

File "/home/me/.virtualenvs/project/local/lib/python2.7/site-packages/django/db/models/query.py", line 429, in bulk_create
    raise ValueError("Can't bulk create a multi-table inherited model")
ValueError: Can't bulk create a multi-table inherited model

and upon inspecting the query.py file, we get this causes the error:

for parent in self.model._meta.get_parent_list():
      if parent._meta.concrete_model is not self.model._meta.concrete_model:
           raise ValueError("Can't bulk create a multi-table inherited model")

Environment

  • Django Model Utils version: 3.1.1
  • Django version: 1.11.7
  • Python version: 2.7.3

Example

PostNotification.objects.bulk_create(
   [PostNotification(related_user=user, post=instance) for user in users]
)

throws the above exception

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7

github_iconTop GitHub Comments

6reactions
aldenjenkinscommented, Apr 13, 2018

CORRECTION: IT WAS NOT ACTUALLY FIXED!

running Notification.objects.bulk_create([SubClassNotification(<vals>),SubClassNotification(<vals>)] only creates base class Notification objects and subsequently running SubClassNotification.objects.all() returns an empty queryset.

1reaction
aldenjenkinscommented, Dec 5, 2018

I believe the issue is impossible to fix. My solution months ago was to redo the wntire notification backend and just use a single Notification() model with a type char selection field to differentiate the type of notification that it was

Read more comments on GitHub >

github_iconTop Results From Across the Web

Django "ValueError: Can't bulk create a multi-table inherited ...
The trick is create a model (which is not an inherited one) dynamically that has some meta (db_table) set. And use this dynamic...
Read more >
#24997 (Allow bulk_create with proxy inheritance) – Django
_meta.parents: raise ValueError("Can't bulk create an inherited model"). This test does not discriminate between multi-table inheritance and proxy ...
Read more >
Can't bulk create a multi-table inherited model"-django
Found a hacky solution. I hope it works in your case. The trick is create a model (which is not an inherited one)...
Read more >
bulk_create for multi table inheritance support - Google Groups
For sqlite we can only support bulk_create if the model does not have a parent with an AutoField. for MySQL I think we...
Read more >
ValueError: Can't bulk create a multi-table inherited model
ValueError : Can't bulk create a multi-table inherited model. ... Model) class which I use to create many notification subclasses such as ...
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