Is there a way to reference multiple fields in a many to one relation?

I have this schema
export const users = pgTable('users', {
id:uuid('id').primaryKey().notNull().defaultRandom(),
})

export const privateChats = pgTable('private_chats', {
id:uuid('id').primaryKey().notNull().defaultRandom(),
userId:uuid('user_id').references(() => users.id),
chatPartnerId:uuid('chat_partner_id').references(() => users.id),
})

export const privateChatsRelations = relations(privateChats, ({ one }) => ({
user: one(users, {
fields: [privateChats.userId],
references: [users.id],
}),
chatPartner: one(users, {
fields: [privateChats.chatPartnerId],
references: [users.id],
}),
}))

export const userRelations = relations(users, ({many }) => ({
privateChats: many(privateChats),
}))
export const users = pgTable('users', {
id:uuid('id').primaryKey().notNull().defaultRandom(),
})

export const privateChats = pgTable('private_chats', {
id:uuid('id').primaryKey().notNull().defaultRandom(),
userId:uuid('user_id').references(() => users.id),
chatPartnerId:uuid('chat_partner_id').references(() => users.id),
})

export const privateChatsRelations = relations(privateChats, ({ one }) => ({
user: one(users, {
fields: [privateChats.userId],
references: [users.id],
}),
chatPartner: one(users, {
fields: [privateChats.chatPartnerId],
references: [users.id],
}),
}))

export const userRelations = relations(users, ({many }) => ({
privateChats: many(privateChats),
}))
I want users.privateChats to be a complete list of chats including ones where privateChats.userId and privateChats.chatPartnerId are the users id, I know for this particular example I should probably create a different data model but in my real example (which is over 100 lines so I won't share it) it's not really possible, is there any way to achieve this?
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server