我可以信任 Django 的 bulk_create 来保持秩序吗?
Can I trust Django's bulk_create to preserve order?
当我做这样的事情时:
model_list = [Model(name = 'First'), Model(name = 'Second'), Model(name = 'Third')]
Model.objects.bulk_create(model_list)
我可以相信它们会按照相同的顺序创建吗?
即:
pk1 = Model.objects.get(name = 'First').pk
pk2 = Model.objects.get(name = 'Second').pk
pk3 = Model.objects.get(name = 'Third').pk
(pk3 > pk2) and (pk2 > pk1)
我想你可以。
model_list = [Model(name = 'First'),
Model(name = 'Second'),
Model(name = 'Third')]
Model.objects.bulk_create(model_list)
此代码将翻译成以下内容SQL:
INSERT INTO app_model (name) VALUES ('First'), ('Second'), ('Third')
常规 SQL 服务器不太可能以不同的顺序插入这些行。
当我做这样的事情时:
model_list = [Model(name = 'First'), Model(name = 'Second'), Model(name = 'Third')]
Model.objects.bulk_create(model_list)
我可以相信它们会按照相同的顺序创建吗?
即:
pk1 = Model.objects.get(name = 'First').pk
pk2 = Model.objects.get(name = 'Second').pk
pk3 = Model.objects.get(name = 'Third').pk
(pk3 > pk2) and (pk2 > pk1)
我想你可以。
model_list = [Model(name = 'First'),
Model(name = 'Second'),
Model(name = 'Third')]
Model.objects.bulk_create(model_list)
此代码将翻译成以下内容SQL:
INSERT INTO app_model (name) VALUES ('First'), ('Second'), ('Third')
常规 SQL 服务器不太可能以不同的顺序插入这些行。