db.update fails with undefined 'relation.referencedTable'

Running into a strange undefined error when running db.update on the lone table in my SQLite database.
app.put("/todos/:id", async (c) => {
const { id } = c.req.param();
const existing = await db.query.todos.findFirst({ with: { id } });
if (existing) {
await db
.update(todos)
.set({ complete: !existing.complete })
.where(eq(todos.id, id));
}
const result = await db.query.todos.findMany();
return c.html(<Todos todos={result} />);
});
app.put("/todos/:id", async (c) => {
const { id } = c.req.param();
const existing = await db.query.todos.findFirst({ with: { id } });
if (existing) {
await db
.update(todos)
.set({ complete: !existing.complete })
.where(eq(todos.id, id));
}
const result = await db.query.todos.findMany();
return c.html(<Todos todos={result} />);
});
The offending lines are db.update(todos).set... where db is the connection to the database through Drizzle, and todos is a table defined by:
export const todos = sqliteTable("todos", {
id: text("id").primaryKey(),
name: text("name").notNull(),
complete: integer("complete", { mode: "boolean" }).notNull().default(false),
});
export const todos = sqliteTable("todos", {
id: text("id").primaryKey(),
name: text("name").notNull(),
complete: integer("complete", { mode: "boolean" }).notNull().default(false),
});
I can't find anyone who's run into this same issue, hoping someone has some insight on whether I've screwed up the syntax here.
No description
4 Replies
Angelelz
Angelelz13mo ago
This is a common error when you don't define or pass all of the relations properly to the drizzle function The relations have to be defined on both sides
tas
tasOP13mo ago
That’s the thing - there are no relations in my db. It’s only the todos table. Do I need to pass some empty list of relations?
Angelelz
Angelelz13mo ago
In that case, this is the source of your error:
const existing = await db.query.todos.findFirst({ with: { id } });
const existing = await db.query.todos.findFirst({ with: { id } });
I think that you meant to write a where clause there. The with is to bring in your related tables I'm surprised you're not seeing a type error in that line
tas
tasOP13mo ago
Oh. I see! Thank you so much. I’ll go give that a try
Want results from more Discord servers?
Add your server