DT
Drizzle Teamā€¢5mo ago
Timo

Query causes error

const user = await database.query.users.findFirst({
where: eq(users.id, id),
with: {
links: true,
reactions: true,
friendships: true
}
})
const user = await database.query.users.findFirst({
where: eq(users.id, id),
with: {
links: true,
reactions: true,
friendships: true
}
})
Is there something wrong? Thanks.
11 Replies
rphlmr āš”
rphlmr āš”ā€¢5mo ago
Looks good (if these relations exist). Any particular error?
Timo
TimoOPā€¢5mo ago
well, the catch-part of a try-catch-block is entered and run. user is never set Error: There are multiple relations between "friendships" and "users". Please specify relation name here this is the error
//// Friendship Model
export const friendships = pgTable(
'friendships',
{
userId: uuid('user_id').references(() => users.id),
anotherUserId: uuid('another_user_id').references(() => users.id),
createdAt: timestamp('created_at').notNull().defaultNow()
},
(table) => ({
primaryKey: primaryKey({
columns: [table.userId, table.anotherUserId]
})
})
)

//// Friendship Relations
export const friendshipsRelations = relations(friendships, ({ one }) => ({
user: one(users, {
fields: [friendships.userId],
references: [users.id]
}),
anotherUser: one(users, {
fields: [friendships.anotherUserId],
references: [users.id]
})
}))
//// Friendship Model
export const friendships = pgTable(
'friendships',
{
userId: uuid('user_id').references(() => users.id),
anotherUserId: uuid('another_user_id').references(() => users.id),
createdAt: timestamp('created_at').notNull().defaultNow()
},
(table) => ({
primaryKey: primaryKey({
columns: [table.userId, table.anotherUserId]
})
})
)

//// Friendship Relations
export const friendshipsRelations = relations(friendships, ({ one }) => ({
user: one(users, {
fields: [friendships.userId],
references: [users.id]
}),
anotherUser: one(users, {
fields: [friendships.anotherUserId],
references: [users.id]
})
}))
this is the data model you will be able to see what the problem is šŸ˜‰ I don't know it ...
rphlmr āš”
rphlmr āš”ā€¢5mo ago
on both relations user and anotherUser, just add a relationName :p
Timo
TimoOPā€¢5mo ago
could you please show me an example? relationName? I am not used to it
rphlmr āš”
rphlmr āš”ā€¢5mo ago
export const friendshipsRelations = relations(friendships, ({ one }) => ({
user: one(users, {
fields: [friendships.userId],
references: [users.id],
relationName: "user"
}),
anotherUser: one(users, {
fields: [friendships.anotherUserId],
references: [users.id],
relationName: "anotherUser"
})
}))
export const friendshipsRelations = relations(friendships, ({ one }) => ({
user: one(users, {
fields: [friendships.userId],
references: [users.id],
relationName: "user"
}),
anotherUser: one(users, {
fields: [friendships.anotherUserId],
references: [users.id],
relationName: "anotherUser"
})
}))
yes when you reference the same columns, you have to add a unique relationName (what you want here)
Timo
TimoOPā€¢5mo ago
and then? should I reset the database? or what? because the error does not disappear
rphlmr āš”
rphlmr āš”ā€¢5mo ago
no but I am creating an example based on your snippets
Timo
TimoOPā€¢5mo ago
thank you are you having some success?
rphlmr āš”
rphlmr āš”ā€¢5mo ago
https://drizzle.run/xnsii9efjoyhovvspftc3j4w yeah sorry I was busy haha I have just reworded some stuff but I hope it helps your seems to be in this use case https://orm.drizzle.team/docs/rqb#disambiguating-relations
Timo
TimoOPā€¢5mo ago
thank you, but now I have the error: Cannot read properties of undefined (reading 'referencedTable') ... ok i solved it thank you again.
rphlmr āš”
rphlmr āš”ā€¢5mo ago
wonderful!
Want results from more Discord servers?
Add your server