Cannot read properties of undefined (reading 'referencedTable')
const result = await db.query.ads.findMany({
with: {
owner: true,
},
})
19 Replies
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.
Can you show how you defined your relations?
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),
}));
That all looks correct. The last point of error would be how you are importing the relations and passing them to drizzle?
// ads.ts
import { users } from './index'
// users.ts
import { ads } from './index'
// index.ts
export { ads } from './ads'
export { users } from './users'
How are you importing your relations and passing the schema to drizzle?
How are you generating your db object?
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?
When you do
And the tables too
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
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'
Because you probably aren't exporting the relations in you index.ts
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
You can also see how they import everything from the 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:
Or something similar, I don't really remember
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
Yeah but you didn't export the relations here
what can i do ?
what can i do ?
Export the relations:
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.
Yes, check it out. But it's ok. It's great you were able to fix your issue