Hi everyone!
I’m working on an API where the client generates the UUIDs for entities, and the backend should either create or update the entity with that ID. However, I’m running into a strange issue:
• When I fetch the entity by ID, sometimes it returns nil (meaning the backend thinks it doesn’t exist).
• So I create a new model instance with that ID and call save().
• But Postgres throws a primary key violation error, which suggests the record does exist.
I tried implementing a “safe retry” function that catches this duplicate key error, then fetches the entity again, and calls update() to apply changes. However, this crashes because Fluent performs a precondition check on the model’s ID property (which is wrapped in a property wrapper), and this check expects the model to already be “tracked” as existing.
My question is:
Is there a recommended Fluent pattern or approach for safely doing a create-or-update by client-generated ID that handles this race condition? Or is there a way to bypass or reset that ID “existence” precondition so I can safely call update() after catching the conflict?
Thanks in advance!
!