SpacetimeDB icon indicating copy to clipboard operation
SpacetimeDB copied to clipboard

Fix issue with indexes not properly updating in SDKs if no primary_key is specified

Open cloutiertyler opened this issue 1 year ago • 0 comments

From @SteveBoytsun:

It took me 2+ hours of torturing stdb, but I finally found a repro. Here are the steps:

  1. Define a table with unique column and a non-unique column (NO PK)
  2. Insert some values into said table
  3. Subscribe to that table
  4. Call a reducer that modifies non-unique values
  5. !Profit The issue happens because
  • Client SDK doesn't treat these changes as updates - instead they're delete+insert
  • Due to how we calculate diffs on the client, inserts are performed before deletes, which means that indexes are updated and subsequently deleted A workaround for that issue is to always have a PK. As for proper fix, I would recommend making sure that we do all deletes BEFORE inserts.

Incidentally, this is part of what I did last week to optimize chunk crossing in BitCraft. I discussed it with @cloutiertyler as purely an optimization, and he suggested sitting on it until after 0.12, but since there's a bug that can be fixed by those changes it may be worth re-evaluating when to do it. Changes are ~2.5/5 on complexity scale (and it's not trivial to separate them from the rest of optimizations), but they are already somewhat-tested in BitCraft.

Note: this may also be an issue in other client SDKs

cloutiertyler avatar Sep 23 '24 23:09 cloutiertyler