Related object is not typed correctly

Hhachoter5/21/2023
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
KKairu5/21/2023
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 })
Hhachoter5/21/2023
@Kairu I am already doing that, everything is exported from the same place so I know it's not missing
Bbloberenober5/21/2023
you need to define the reverse relation side, otherwise, there is no way to know which columns define this relation
Hhachoter5/21/2023
@Dan Kochetov do I always have to define both sides of the relation? What if I only ever want to query from one side?
Bbloberenober5/21/2023
not always, but in this case you didn't specify the columns
Hhachoter5/21/2023
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
Bbloberenober5/21/2023
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

Looking for more? Join the community!

Want results from more Discord servers?
Add your server
Recommended Posts
Custom vector type with pgvectorI'm trying to use the `pgvector` extension with a custom type but am running into an issue with the Missing 'with' clause additional operators (where, limit, offset, etc)Have been digging into the docs and the latest update. ❣️ In the docs (https://orm.drizzle.team/ddrizzle-zod type errors with latest versionsI updated all drizzle deps to latest and having type errors when using drizzle-zodI am confused on how the new relational queries worksIn the docs I see the following ``` import { pgTable, serial, text, integer, boolean } from 'drizzleis not assignable to type 'DrizzleD1Database'Hello This last release was amazing. Making joins simple was the missing piece for me. However, IDrizzle kit generate gives error after upgradeI have updated drizzle-orm to 0.26.0 and drizzle-kit to 018.0. I have defined relations according toIntrospection error with pgHello, I am trying to introspect my already created DB but when I run the cli command I get: ``` > dHow to declare PostgreSQL extensions/plugin?Reference: https://www.prisma.io/docs/concepts/components/prisma-schema/postgresql-extensions Is thHow to consume existing supabase migration?I have a supabase migrations and would like to reuse them as a starter for drizzle. When trying to pPrisma Studio style db explorerHey there! Anyone aware of a prisma studio style db-playground / db-admin-dashboard that works withIncorrect return type findFristHi! I just upgraded to `26.0` and I'm refactoring queries to `findFirst` where needed. But the retuRelations module - or conditionHi there! I have a teams table, an users table, and a team_members table that connects these two. AAre foreign key polyfills for PlanetScale supported by Drizzle?Hi Prisma supports this (although slow). See: https://www.prisma.io/docs/guides/database/planetscalTS error: Argument of type PgTableWithColumns is not assignable to parameter of type AnyPgTableHi all, after updating to latest i'm getting this TS error. This is my db creation and schema. ``` Many-to-many relational query issuesschema, relations, statements: https://gist.github.com/kylewardnz/37104f989807e96555ea856294a2b670 drizzle-kit: push wants to change column type that hasn't changedjust updated my dependencies: ``` dependencies: - drizzle-orm 0.25.4 + drizzle-orm 0.26.0 devDependedrizzle-kit doesn't seems picking up the default config TS (up:pg)Hi I'd like to report that the latest drizzle-kit `0.18.0` doesn't seems to detect my `drizzle.confBuild queries dynamically with conditions.How to build query on the basis of if-else conditions? I have tried doing it as below ``` let dbQuer