@@ -384,7 +384,7 @@ def upsert_entity_kinds(self, entity_kinds):
384
384
# Select all our existing entity kinds for update so we can do proper locking
385
385
# We have to select all here for some odd reason, if we only select the ones
386
386
# we are syncing we still run into deadlock issues
387
- list (EntityKind .all_objects .all ().select_for_update ().values_list ('id' , flat = True ))
387
+ list (EntityKind .all_objects .all ().order_by ( 'id' ). select_for_update ().values_list ('id' , flat = True ))
388
388
389
389
# Upsert the entity kinds
390
390
upserted_enitity_kinds = manager_utils .bulk_upsert (
@@ -412,7 +412,7 @@ def upsert_entities(self, entities, sync=False):
412
412
if entities :
413
413
# Default select for update query when syncing all
414
414
select_for_update_query = (
415
- 'SELECT FROM {table_name} FOR NO KEY UPDATE'
415
+ 'SELECT FROM {table_name} ORDER BY id ASC FOR NO KEY UPDATE'
416
416
).format (
417
417
table_name = Entity ._meta .db_table
418
418
)
@@ -421,7 +421,10 @@ def upsert_entities(self, entities, sync=False):
421
421
# If we are not syncing all, only select those we are updating
422
422
if not sync :
423
423
select_for_update_query = (
424
- 'SELECT FROM {table_name} WHERE (entity_type_id, entity_id) IN %s FOR NO KEY UPDATE'
424
+ 'SELECT FROM {table_name} '
425
+ 'WHERE (entity_type_id, entity_id) IN %s '
426
+ 'ORDER BY id ASC '
427
+ 'FOR NO KEY UPDATE'
425
428
).format (
426
429
table_name = Entity ._meta .db_table
427
430
)
@@ -506,7 +509,7 @@ def upsert_entity_relationships(self, queryset, entity_relationships):
506
509
507
510
# Select the relationships for update
508
511
if entity_relationships :
509
- list (queryset .select_for_update ().values_list (
512
+ list (queryset .order_by ( 'id' ). select_for_update ().values_list (
510
513
'id' ,
511
514
flat = True
512
515
))
0 commit comments