drizzle-team / drizzle-team/drizzle-orm

[BUG]: useLiveQuery doesn't see change when querying joined tables

Open
#2,660 14 comments 20 reactions 0 assignees View on GitHub
bug driver/expo-sqlite priority
Dominant language
TypeScript
Stars
35.8k
Forks
1.6k
Avg merge
2d 7h
Merged PRs (30d)
4

Description

### What version of `drizzle-orm` are you using?

0.31.2

### What version of `drizzle-kit` are you using?

0.22.5

### Describe the Bug

I'm not positive if this is a bug or a feature request, but the docs around `useLiveQuery` should probably mention this if it's not currently supported.

1. Setup a live query
```ts
const { data } = useLiveQuery(
db
.select()
.from(booksToAuthors)
.leftJoin(booksTable, eq(booksToAuthors.bookId, booksTable.id))
.leftJoin(authors, eq(booksToAuthors.authorId, authors.id))
);
```
2. Make some update to one of the joined tables
```ts
await db
.update(booksTable)
.set({ title: "newTitle" })
.where(eq(books.id, "someBookId"))
.execute();
```

Actual behavior:
`data` is not updated, but I can see the change if I force quit and relaunch.

Other details:
The only workaround I've really found has been to abandon joins in the query and instead set up separate live queries for each table. Then I can do the lookup join in Javascript with a `reduce` function, but I'd really love to be able to just use the sql methods here.

### Expected behavior

`useLiveQuery` would see that one of the tables that informs the query has changed, and `data` would be updated accordingly.

### Environment & setup

package.json
```json
"expo": "~51.0.0",
"react-native": "0.74.2",
"expo-sqlite": "^14.0.3",
```

schema.ts
```ts
export const books = sqliteTable('books', {
id: text('id').primaryKey(),
title: text('title').notNull(),
});

export const booksRelations = relations(books, ({ many }) => ({
booksToAuthors: many(booksToAuthors),
}));

export const authors = sqliteTable('authors', {
id: integer('id').primaryKey(),
name: text('name').notNull().unique(),
});

export const authorsRelations = relations(authors, ({ many }) => ({
booksToAuthors: many(booksToAuthors),
}));

export const booksToAuthors = sqliteTable(
'books_to_authors',
{
bookId: text('book_id').notNull().references(() => books.id),
authorId: integer('author_id').notNull().references(() => authors.id),
},
(t) => ({
pk: primaryKey({ columns: [t.bookId, t.authorId]})
})
);

export const booksToAuthorsRelations = relations(booksToAuthors, ({ one }) => ({
authors: one(authors, {
fields: [booksToAuthors.authorId],
references: [authors.id],
}),
books: one(books, {
fields: [booksToAuthors.bookId],
references: [books.id],
}),
}));
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.