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')
});
6 Replies
paaradiso
paaradisoOP14mo ago
oh i'm dumb, i can't declare relations like this?
Ahmed
Ahmed14mo 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
paaradiso
paaradisoOP14mo ago
can i have both the references i currently have and these new relations?
Ahmed
Ahmed14mo 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
paaradiso
paaradisoOP14mo 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
Angelelz
Angelelz14mo 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