Reputation: 9637
my_model_instance = MyModel.objects.select_for_update().get(id=1)
something_related = my_model_instance.related_thing
In the above code, related_thing
is another model instance related to my_model_instance
by a foreign key. Since I originally did a select_for_update()
, will the related_thing
be locked as well?
Upvotes: 2
Views: 1328
Reputation: 2519
No. The example you give will make 2 queries:
SELECT ••• FROM `mymodel` WHERE `mymodel`.`id` = 1 FOR UPDATE;
SELECT ••• FROM `related_thing` WHERE `related_thing`.`id` = ?;
Clearly only the first locks. However, if you use select_related() the single query would be:
SELECT ••• FROM `mymodel` LEFT OUTER JOIN `related_thing` ON (`mymodel`.`related_thing_id` = `related_thing`.`id`) WHERE `mymodel`.`id` = 1 FOR UPDATE;
In which case it would lock related_thing as well (at least in MySQL but it might be db dependent).
Upvotes: 2