Related object is not typed correctly

I have the following schema
export const menus = pgTable('menus', {
id: uuid('id').primaryKey(),
name: varchar('name', { length: 256 }).notNull(),
});

// categories
export const categories = pgTable('categories', {
id: uuid('id').primaryKey(),
name: varchar('name', { length: 256 }).notNull(),
menuId: uuid('menu_id').references(() => menus.id),
});

export const menuCategories = relations(menus, ({ many, one }) => {
return {
categories: many(categories)
};
});
export const menus = pgTable('menus', {
id: uuid('id').primaryKey(),
name: varchar('name', { length: 256 }).notNull(),
});

// categories
export const categories = pgTable('categories', {
id: uuid('id').primaryKey(),
name: varchar('name', { length: 256 }).notNull(),
menuId: uuid('menu_id').references(() => menus.id),
});

export const menuCategories = relations(menus, ({ many, one }) => {
return {
categories: many(categories)
};
});
And I am querying like this
async function main(){
const res = await db.query.menus.findMany({
with:{
categories:true
}
})

const cat = res[0].categories[0]
}
async function main(){
const res = await db.query.menus.findMany({
with:{
categories:true
}
})

const cat = res[0].categories[0]
}
cat is of type
{
[x:string]:any
}
{
[x:string]:any
}
I was expecting it to be typed correctly
7 Replies
Kairu
Kairu2y ago
you need to pass your schema into drizzle() as the second argument
import * as schema from "./schema"
export const db = drizzle(connection, { schema })
import * as schema from "./schema"
export const db = drizzle(connection, { schema })
hachoter
hachoterOP2y ago
@Kairu I am already doing that, everything is exported from the same place so I know it's not missing
Dan
Dan2y ago
you need to define the reverse relation side, otherwise, there is no way to know which columns define this relation
hachoter
hachoterOP2y ago
@Dan Kochetov do I always have to define both sides of the relation? What if I only ever want to query from one side?
Dan
Dan2y ago
not always, but in this case you didn't specify the columns
hachoter
hachoterOP2y ago
Can I just specify the fields in the many relation? I'm just gonna write some helper functions to automatically generate it, I hope I can make it type safe though
Dan
Dan2y ago
you need to define the columns on the relation side that actually has those columns the many side doesn't have columns that define the relation
Want results from more Discord servers?
Add your server