Nozbe / WatermelonDB

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

find() returns deleted records #1830

Open dcheli opened 2 months ago

dcheli commented 2 months ago

I recently discovered that if I execute a find() on a record collection using an id for a record that has been previously deleted with markAsDeleted(), the query will return the record.

for example: const goal = await goalCollection.find(goalActivity.goal.id);

will return a previously deleted goal. I have 2 questions about this:

  1. Is this expected behavior?
  2. If it is expected behavior, what is the best course of action to determine if the record is deleted? I can look at the _raw._status field, but accessing the _raw property directly seems to be frowned upon. Thanks
dcheli commented 2 months ago

Also, I'm using "@nozbe/watermelondb": "^0.27.1",

KrisLau commented 2 months ago

It is not expected behavior. Are you syncing your records or resolving syncs in some way?

If you only use Watermelon as a local database, destroy records permanently, if you synchronize, mark as deleted instead.

await somePost.markAsDeleted() // syncable
await somePost.destroyPermanently() // permanent

https://watermelondb.dev/docs/CRUD#delete-a-record

dcheli commented 2 months ago

Thanks for the response and confirming that this is not expected behavior. I am not currently syncing my records, but that is the long term , well, in the next few months, hence I'm using markAsDeleted(). What seems to work rather then using the find(), is to just use:

.query(Q.where('my_field_id', someid))

If the record doesn't exist, it returns an empty array.