Django is created in batches using bulk_create, and the returned objects are not contended (primary key)

use django"s bulk_create for bulk insert operations (no primary key specified) to return a list of objects, where the object primary key is None.

is there any way to carry competition in the results returned after a batch operation?

Mar.16,2021

answer it yourself. Look at the source code of django1.7 bulk_create. There is an interesting comment on it:

-sharp So this case is fun. When you bulk insert you don't get the primary
-sharp keys back (if it's an autoincrement), so you can't insert into the
-sharp child tables which references this. There are two workarounds, 1)
-sharp this could be implemented if you didn't have an autoincrement pk,
-sharp and 2) you could do it by doing O(n) normal inserts into the parent
-sharp tables to get the primary keys back, and then doing a single bulk
-sharp insert into the childmost table. Some databases might allow doing
-sharp this by using RETURNING clause for the insert query. We're punting
-sharp on these for now because they are relatively rare cases.

there are two solutions: 1) the primary key is not set to self-growing, which means you need to specify the value of the primary key yourself. Do you understand it correctly?
2) insert one by one honestly

think about it or insert it one by one and put it into the transaction. I don't know if there is a better way

Menu