Cannot read properties of undefined (reading 'referencedTable')

i'm just playing around with queries and i can't get this to work:
const liked = await db.query.collectionsTable.findMany({
with: {
likedCollectionsTable: true
}
});
const liked = await db.query.collectionsTable.findMany({
with: {
likedCollectionsTable: true
}
});
without the with it works. i'm passing the schemas to the db object:
export const db = drizzle(queryClient, { schema });
export const db = drizzle(queryClient, { schema });
and here are the two schemas:
export const likedCollectionsTable = pgTable('liked_collections', {
userId: text('user_id').references(() => usersTable.id),
collection_id: integer('collection_id').references(() => collectionsTable.id)
});

export const collectionsTable = pgTable('collections', {
id: integer('id').primaryKey(),
name: text('name'),
type: collectionTypeEnum('type'),
cover: text('cover'),
artist: text('artist'),
tags: text('tags')
.references(() => tagsTable.name)
.array(),
releaseDate: date('release_date'),
addedBy: text('added_by')
});
export const likedCollectionsTable = pgTable('liked_collections', {
userId: text('user_id').references(() => usersTable.id),
collection_id: integer('collection_id').references(() => collectionsTable.id)
});

export const collectionsTable = pgTable('collections', {
id: integer('id').primaryKey(),
name: text('name'),
type: collectionTypeEnum('type'),
cover: text('cover'),
artist: text('artist'),
tags: text('tags')
.references(() => tagsTable.name)
.array(),
releaseDate: date('release_date'),
addedBy: text('added_by')
});
P
paaradiso178d ago
oh i'm dumb, i can't declare relations like this?
A
Ahmed178d ago
To use the query API, you should add the relation using relations in the schema file. Check this out: https://orm.drizzle.team/docs/rqb#declaring-relations
Drizzle Queries - DrizzleORM
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind
P
paaradiso178d ago
can i have both the references i currently have and these new relations?
A
Ahmed178d ago
You did define it in the database level, so the foreign key constraint will be enforced. But drizzle requires some configuration to use their query selector. Here's a quote from drizzle: "You might've noticed that relations look similar to foreign keys — they even have a references property. So what's the difference? While foreign keys serve a similar purpose, defining relations between tables, they work on a different level compared to relations. Foreign keys are a database level constraint, they are checked on every insert/update/delete operation and throw an error if a constraint is violated. On the other hand, relations are a higher level abstraction, they are used to define relations between tables on the application level only. They do not affect the database schema in any way and do not create foreign keys implicitly. What this means is relations and foreign keys can be used together, but they are not dependent on each other. You can define relations without using foreign keys (and vice versa), which allows them to be used with databases that do not support foreign keys, like PlanetScale." You should define both in your case
P
paaradiso178d ago
gotcha, thanks what i'm trying to get is this:
{
id: 12345678,
name: 'A',
type: 'album',
cover: 'HNpgYw8UdhlJ6JHq.avif',
artist: 'A2',
tags: [
'thall', 'deathcore',
'metalcore', 'black metal',
'hardcore', 'avant-garde',
'french', 'death metal',
'australian', 'calle',
'blackened', 'melodic'
],
releaseDate: '3333-03-31',
addedBy: 'paaradiso',
likes: [ '187510439066730496', '1015654795731816580' ]
}
{
id: 12345678,
name: 'A',
type: 'album',
cover: 'HNpgYw8UdhlJ6JHq.avif',
artist: 'A2',
tags: [
'thall', 'deathcore',
'metalcore', 'black metal',
'hardcore', 'avant-garde',
'french', 'death metal',
'australian', 'calle',
'blackened', 'melodic'
],
releaseDate: '3333-03-31',
addedBy: 'paaradiso',
likes: [ '187510439066730496', '1015654795731816580' ]
}
and this is the code i'm using to get it:
const query = await db
.select()
.from(collectionsTable)
.where(eq(collectionsTable.id, id))
.rightJoin(likedCollectionsTable, eq(likedCollectionsTable.collection_id, id));
const collectionWithLikes = {
...collection,
likes: query.reduce((acc, { liked_collections: { userId } }) => {
acc.push(userId);
return acc;
}, [])
};
const query = await db
.select()
.from(collectionsTable)
.where(eq(collectionsTable.id, id))
.rightJoin(likedCollectionsTable, eq(likedCollectionsTable.collection_id, id));
const collectionWithLikes = {
...collection,
likes: query.reduce((acc, { liked_collections: { userId } }) => {
acc.push(userId);
return acc;
}, [])
};
where a collection is:
{
id: 12345678,
name: 'A',
type: 'album',
cover: 'HNpgYw8UdhlJ6JHq.avif',
artist: 'A2',
tags: [
'thall', 'deathcore',
'metalcore', 'black metal',
'hardcore', 'avant-garde',
'french', 'death metal',
'australian', 'calle',
'blackened', 'melodic'
],
releaseDate: '3333-03-31',
addedBy: 'paaradiso',
}
{
id: 12345678,
name: 'A',
type: 'album',
cover: 'HNpgYw8UdhlJ6JHq.avif',
artist: 'A2',
tags: [
'thall', 'deathcore',
'metalcore', 'black metal',
'hardcore', 'avant-garde',
'french', 'death metal',
'australian', 'calle',
'blackened', 'melodic'
],
releaseDate: '3333-03-31',
addedBy: 'paaradiso',
}
and liked_collections looks like
{
user_id: 123,
collection_id: 321
}
{
user_id: 123,
collection_id: 321
}
would it be easier with queries? or should i just stick with an array of likes inside the collection? lol
A
Angelelz178d ago
It's up to you. You have both options. Maybe you'd like to benchmark it a see what's faster?
Want results from more Discord servers?
Add your server
More Posts
Key columns "user_id" and "id" are of incompatible types: text and bigint.I'm getting the error in the title ^ but they're the same type. ```js export const usersTable = pgTMigration Failed LibsqlErrorI had migrated prior, done a lot of schema work, and then did a drizzle-generate, worked fine and I[Solved] cannot find package 'mysql2'I've setup drizzle with a planetscale db and pushed a simple user schema, however running the pnpm dError: this.client.prepare is not a functionI am trying to build a project using Bun, Hono and Drizzle. When i am starting the project with thispg: introspect failsSo I have some tables that do not have a primary key constraint (by design) and it seems that drizzlclient connection refusedI'm using bun. I was able to get the pool connection working, but the same connection configuratioRunning migrations in server-less (edge) land??According to the docs: ``` // this will automatically run needed migrations on the database await mIs there any way to call drizzle-kit with the new node --env-file=.env argument?Hey, since node supports env files since pretty recently, it would be nice if we could ditch dotenv.enumerating tablesI have a table of possible 'statuses' for a column in my records table and would like my zod schema how to group Related Items```typescript // index.ts const coursesList = await db .select({ title: courses.title, tags: tagsNested where filter, how to not include emptyThe following almost works ``` const query = db.query.productionItem.findMany({ with: { itemStis placeholder in prepare statements deprecated!??```typescript const courses = db.query.userCourses .findMany({ where: eq(placeholder("userId")SQLite json_eachHi. I wandted to use the json_each function in sqlite, but I dont think the query is building correcForeign key truncated for being too long?Hi, I have multiple warnings / notices: `{ severity_local: 'NOTICE', severity: 'NOTICE', codedrizzle-zod with custom typesI have defined opaque types in my application and have implemented those in the drizzle schema (awesUsing transactions in automated testing like JestI have a testing set-up as follows: 1. Start postgres container 2. Apply migrations to db 3. Jest tTypeScript complaining about using a spread operator in partial select queryTypeScript complaining about me using a spread operator in partial select query. I have the followiUnable to read error message when inserting withdb.insert().values()Hey there, i am using visual studio code and am developing with nuxt3. I am trying to insert an objehow to add new driver to drizzle?what is the procedure of adding a new driver to drizzle orm?`drizzle-kit --custom` is not giving me a blank migration fileI'm doing the following: ```sh drizzle-kit generate:pg --custom ``` But it's not giving me a blank