instantdb / instant

The realtime client-side database
https://instantdb.com
Apache License 2.0
5.81k stars 133 forks source link

`lookup` issue #42

Closed ayoung19 closed 6 days ago

ayoung19 commented 3 weeks ago

I'm getting this error:

Unhandled Runtime Error
Error: playerId is not a unique attribute.

Source
src/components/App.tsx (43:8) @ transact

  41 |
  42 |   useEffect(() => {
> 43 |     db.transact([
     |        ^
  44 |       tx.movements[lookup("playerId", user.id)].update({
  45 |         x: movement.x,
  46 |         y: movement.y,

trying to do this write operation (link):

    db.transact([
      tx.movements[lookup("playerId", user.id)].update({
        x: movement.x,
        y: movement.y,
      }),
    ]);

with the following schema (link):

import { i } from "@instantdb/core";

const graph = i.graph(
  "c7a8aae8-5bfc-43e7-9be1-6f439475a114",
  {
    movements: i.entity({
      playerId: i.string().unique().indexed(),
      x: i.number(),
      y: i.number(),
    }),
  },
  {}
);

export default graph;

What am I doing wrong here? I'm simply trying to update the movements entity by its unique playerId attribute. As a sanity check, here are the attribute settings in the dashboard:

image
dwwoelfel commented 2 weeks ago

The underlying issue is that the db.transact happens before the client has fetched its attributes and so it doesn't know that it's a unique attribute.

Working on a fix.

dwwoelfel commented 2 weeks ago

This should be fixed in 0.12.9. Please reopen and ping me if you're still having issues.

Zeb88 commented 1 week ago

This should be fixed in 0.12.9. Please reopen and ping me if you're still having issues.

This issue seems to persist in "@instantdb/react": "^0.12.14". Here's my query -

instantDb.transact([
    tx.readings[lookup('positionId', reading.positionId)].update(reading).link({ jobs: reading.jobId }),
 ]);
dwwoelfel commented 1 week ago

@Zeb88 what's the error that you're seeing here?

dwwoelfel commented 6 days ago

Should be fixed by https://github.com/instantdb/instant/pull/161 in version 12.0.27. Please @ me if you're still seeing the issue after upgrading.