Nozbe / WatermelonDB

🍉 Reactive & asynchronous database for powerful React and React Native apps ⚡️
https://watermelondb.dev
MIT License
10.49k stars 589 forks source link

`updated` record being marked as `created` #1141

Open MikeGlennFWC opened 3 years ago

MikeGlennFWC commented 3 years ago

Hello,

I have a local device record that is created, then moments later is updated again by the device.

The created record hits the server and syncs fine.

Then the record is updated on the local device, but the _status column is marked as created

This tries to send the record again on next sync as created, but this should be in the updated list. The server rejects the sync because it finds a duplicate key in the database as it tries to insert with the same ID but updated values.

I have a screenshot showing the status of created but it showing 2 columns updated.

Is this something to do with the way i have set up WatermelonDB, or is this a bug? I'm using version 0.23.0

Screenshot 2021-08-17 at 15 16 50
radex commented 3 years ago

no, this is expected behavior on WatermelonDB side and a bug on your backend, however it's likely that it's been obscured before and now visible because of 0.23 changes.

record stays created most likely because it was changed between push beginning and completion. however, your server should treat created and updated records essentially the same way - the distinction is kind of arbitrary at this point. https://github.com/Nozbe/WatermelonDB/blob/master/docs-master/Advanced/Sync.md#implementing-push-endpoint

MikeGlennFWC commented 3 years ago

Ok, thanks. I'll update to follow this.

One more thing, in your backend implementations, do you have _changes and _status fields on your backend database?

radex commented 3 years ago

One more thing, in your backend implementations, do you have _changes and _status fields on your backend database?

No

MikeGlennFWC commented 3 years ago

Sorry, I'm over thinking it, of course you don't.

I'm now upserting created records, which fixes the issue.

last question, is WatermelonDB ok with receiving the same record in created changes on a pull, even if it's already created that record? because that will happen in the above instance if it's upserted.

It used to throw a warning, but safely updated it.

radex commented 3 years ago

last question, is WatermelonDB ok with receiving the same record in created changes on a pull, even if it's already created that record?

yes