How to bulk_create in django using MySQL

Does not return ID when using bulk_create method. PostgreSQL returns the ID, but MySQL does not. Is there a way to create a temporary ID and solve it?

bar_list = []
foo_list = []
for i in range(3):
    foo = Foo(body="text")
    foo.temp_id = uuid.uuid4()

    bar = Bar(foo_temp_id=foo.temp_id, body="text")

Foo.objects.bulk_create(foo_list)  # The query sets are created, but the ID is None
Bar.objects.bulk_create(bar_list)  # Error occurred because the ID of the referring foo is None


  • This is one of the documented limitations of django ORM's bulk_create method if the id field is AutoField:

    If the model’s primary key is an AutoField, the primary key attribute can only be retrieved on certain databases (currently PostgreSQL, MariaDB 10.5+, and SQLite 3.35+). On other databases, it will not be set.

    You either have to provide the id explicitly when you create the Foo objects or you cannot use bulk_create with mysql in this instance.