Cannot read properties of undefined (reading 'referencedTable')

const result = await db.query.ads.findMany({ with: { owner: true, }, })
19 Replies
francel
francelOP14mo ago
I can't retrieve the ads with their user, when I remove the with: the query works but with the with: it doesn't work.
Angelelz
Angelelz14mo ago
Can you show how you defined your relations?
francel
francelOP14mo ago
export const ads = pgTable("ads", { id: uuid('id').defaultRandom().notNull().primaryKey(), ownerId: uuid('ownerId'), }); export const adsRelations = relations(ads, ({ one, many }) => ({ owner: one(users, { fields: [ads.ownerId], references: [users.id], }), })); export const users = pgTable("users", { // id: serial("id"), // id: serial('id').primaryKey(), id: uuid('id').defaultRandom().notNull().primaryKey(), // name: text("name"), // fullName: text('full_name').unique(), // phone: varchar('phone', { length: 256 }), email: varchar("email").unique(), }); export const usersRelations = relations(users, ({ many }) => ({ ads: many(ads), }));
Angelelz
Angelelz14mo ago
That all looks correct. The last point of error would be how you are importing the relations and passing them to drizzle?
francel
francelOP14mo ago
// ads.ts import { users } from './index' // users.ts import { ads } from './index' // index.ts export { ads } from './ads' export { users } from './users'
Angelelz
Angelelz14mo ago
How are you importing your relations and passing the schema to drizzle? How are you generating your db object?
francel
francelOP14mo ago
I just follow documentation I use npm run drizzle-kit generate:pg to generate migration I use npm run drizzle-kit push:pg to execute migration Should we import the relationships? to use them where for example?
Angelelz
Angelelz14mo ago
When you do
const db = drizzle(client, { schema }) // <-- All the relations should be in the schema object.
const db = drizzle(client, { schema }) // <-- All the relations should be in the schema object.
And the tables too
francel
francelOP14mo ago
yes, but why the query not working when i use with: to include the relation. https://orm.drizzle.team/docs/rqb#include-relations
Drizzle Queries - DrizzleORM
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind
francel
francelOP14mo ago
I really feel like I wasted all my time migrating from prisma to drizzle. // node-postgres import { drizzle } from "drizzle-orm/node-postgres"; // import { Client } from "pg"; import Pool from 'pg-pool' import * as schema from './schema/index'; // const client = new Client({ // connectionString: process.env.DATABASE_URL, // }); // await client.connect(); // export const db = drizzle(client); const pool = new Pool({ connectionString: process.env.DATABASE_URL_DRIZZLE_ORM, }); export const db = drizzle(pool, { schema, logger: true }); // node-postgres { Client } from 'pg' not working, so i use import Pool from 'pg-pool' When i use import { Client } from "pg"; i get this error SyntaxError: The requested module 'pg' does not provide an export named 'Client' So ise use import Pool from 'pg-pool'
Angelelz
Angelelz14mo ago
Because you probably aren't exporting the relations in you index.ts
Angelelz
Angelelz14mo ago
From here: https://orm.drizzle.team/docs/rqb#querying
Relational queries are an extension to Drizzle's original query builder. You need to provide all tables and relations from your schema file/files upon drizzle() initialization and then just use the db.query API.
Drizzle Queries - DrizzleORM
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind
Angelelz
Angelelz14mo ago
You can also see how they import everything from the schema:
import * as schema from './schema';
import * as schema from './schema';
It's normal to make mistakes when addopting a new technology. I did this switch myself and it took me some time, but I personally feel it was worth it. This is a problem with javascript and node as a whole, the module resolution algorithm is different on different node versions, and it would be different depending on your tsconfig.json I've seen this before as well, sometimes you have to do:
import * as pg from "pg";
const Client = pg.Client;
import * as pg from "pg";
const Client = pg.Client;
Or something similar, I don't really remember
francel
francelOP14mo ago
I changed to using this, but I still get the same error. all the other queries pass, but the one with the with: still doesn't work for me // node-postgres import { drizzle } from "drizzle-orm/node-postgres"; import pg from "pg"; import * as schema from './schema/index'; const client = new pg.Client({ connectionString: process.env.DATABASE_URL_DRIZZLE_ORM, }); (async () => { try { await client.connect(); } catch (error) { console.log(error); await client.end(); } })(); export const db = drizzle(client, { schema, logger: true }); // node-postgres
Angelelz
Angelelz14mo ago
Yeah but you didn't export the relations here
francel
francelOP14mo ago
what can i do ? what can i do ?
Angelelz
Angelelz14mo ago
Export the relations:
// index.ts

export { ads, adsRelations } from './ads'
export { users, usersRelations } from './users'
// index.ts

export { ads, adsRelations } from './ads'
export { users, usersRelations } from './users'
francel
francelOP14mo ago
ah ok, really thank you thank you very much, thank you very much. God bless you for your help. I followed the documentation and it is not shown in the documentation that it is also necessary to import the relationships.
Angelelz
Angelelz14mo ago
Yes, check it out. But it's ok. It's great you were able to fix your issue
Want results from more Discord servers?
Add your server