#28344: Add for_update parameter to Model.refresh_from_db() -------------------------------------+------------------------------------- Reporter: Patryk Zawadzki | Owner: (none) Type: New feature | Status: new Component: Database layer | Version: dev (models, ORM) | Severity: Normal | Resolution: Keywords: | Triage Stage: Accepted Has patch: 0 | Needs documentation: 0 Needs tests: 0 | Patch needs improvement: 0 Easy pickings: 0 | UI/UX: 0 -------------------------------------+-------------------------------------
Comment (by Aivars Kalvāns): Replying to [comment:13 Jure Erznožnik]: > {{{ > with obj.lock_for_update(): > obj.some_attr = 13 > # Would this be auto-saved at the end of the CM's block? > }}} > > However, this one implies both a transaction and an auto-save to me. So my proposal would be to "decorate" the `lock_for_update()` with three parameters: > > * `atomic: bool=True` > * `auto-save: bool=True` > * `refresh_from_db: bool=True` Can you elaborate on what those parameters would do? If I understand `refresh_from_db` correctly - it opens the door to the bug mentioned in comment:10. {{{ foo = Foo.objects.filter(...).get() ... Foo.objects.select_for_update.get(pk=foo.pk) }}} it leads to a situation where the object `foo` differs from the now locked database record. That's why I would like to have a single operation to lock the object and refresh the object with the latest "locked" values from DB is valuable. I'm all for something like `refresh_from_db` or `refresh_for_update` to avoid programming errors and reduce number of database operations at the same time. That said, I have objections to RoR way of `obj.lock_for_update` or `obj.with_lock`: it implies that the lock will be released once the block ends, but that is not the case. Once locked, the record will be locked until the main transaction is committed or rolled back. Nested transactions (savepoints) may release locks when rolled back, but it differs between databases: Oracle does not release locks, but Postgres does (IMHO). It looks nice, but it gives the wrong impression of how it works. -- Ticket URL: <https://code.djangoproject.com/ticket/28344#comment:21> Django <https://code.djangoproject.com/> The Web framework for perfectionists with deadlines. -- You received this message because you are subscribed to the Google Groups "Django updates" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-updates+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/0107018c53baea86-3470d555-7415-47fd-9bd4-0f5e3826f693-000000%40eu-central-1.amazonses.com.