Open bpsoos opened 2 months ago
You shouldn't need to run a GetItem
operation before running an UpdateItem
.
If the existing version of record is say 6
, we shouldn't write the update operation as SET version = 7
but rather SET version = version + 1
That way the increment operation doesn't need to know the previous value. This would also work for the case where there is no version attribute set yet.
Example: dynamodb lets you do this:
UpdateExpression: "SET version = if_not_exists(version, :initial) + :inc",
ExpressionAttributeValues:
":inc": 1,
":initial": 0,
},
The Issue
Given a model with a version attribute:
a simple update on the model without getting it first resets it's version to 1:
manual version update also fails with "Two document paths overlap with each other" error as the
update
method automatically adds aSET
operation for the version and they conflict:The same issues apply to updates in transactions. See the integration tests in the linked pull request for runnable versions of these examples.
Root cause
The root cause seems to be in Model._handle_version_attribute, which automatically tries to increment the models version and sets it to 1 if
getattr(self, self._version_attribute_name)
returnsNone
.Solution proposal
I propose to add an
increment_version
flag to the relevant Model methods, similar toadd_version_condition
. For example: