Reputation: 8762
I'm working with ArangoDB and facing an issue with an AQL upsert operation where the CreateDate field is expected to be set only during insert operations and remain unchanged during updates. However, after performing an upsert, I noticed that the CreateDate field becomes empty for updates, which is not the intended behavior. The issue does not occur during inserts, where CreateDate is correctly set. Here's the relevant part of my code:
UPSERT { _key: entity._key }
INSERT MERGE(entity, { CreateDate: DATE_ISO8601(DATE_NOW()), UpdatedDate: DATE_ISO8601(DATE_NOW()) })
UPDATE MERGE(entity, { UpdatedDate: DATE_ISO8601(DATE_NOW()) })
IN @@collectionName
The CreateDate should only be set when a new document is inserted and should not be modified if the document exists and is being updated. However, during an update operation, CreateDate ends up being empty.
My question:
Why does the CreateDate
field become empty during an update operation, even though my intention is to only modify it during insert operations?
I've confirmed that the issue does not occur during insert operations CreateDate
is correctly set.
Upvotes: 0
Views: 38
Reputation: 781
If your entity
document has the property CreateDate
set to "" or null
during an UPDATE
then that would explain what you're observing - I would confirm the contents of entity
to be sure.
In any event you can use the 'pseudo value' OLD
in your UPDATE
expression to reference the previous (stored) version of the document and guarantee that CreateDate
is preserved as follows:
UPSERT { _key: entity._key }
INSERT MERGE(entity, { CreateDate: DATE_ISO8601(DATE_NOW()), UpdatedDate: DATE_ISO8601(DATE_NOW()) })
UPDATE MERGE(entity, { CreateDate: OLD.CreateDate, UpdatedDate: DATE_ISO8601(DATE_NOW()) })
IN @@collectionName
Check out the UPSERT
documentation for more info.
Upvotes: 0