Multiple relations

Let's say I have this relationship from message to users:
export const MessageRelations = relations(MessageTable, ({ one }) => ({
sender: one(UserTable, {
fields: [MessageTable.sender_id],
references: [UserTable.id],
}),

recipient: one(UserTable, {
fields: [MessageTable.recipient_id],
references: [UserTable.id],
}),
}))
export const MessageRelations = relations(MessageTable, ({ one }) => ({
sender: one(UserTable, {
fields: [MessageTable.sender_id],
references: [UserTable.id],
}),

recipient: one(UserTable, {
fields: [MessageTable.recipient_id],
references: [UserTable.id],
}),
}))
And I'd like to add a relation from user to messages, by the sender. I thought of doing this:
export const UserRelations = relations(UserTable, ({ many }) => ({
messages: many(MessageTable),
}))
export const UserRelations = relations(UserTable, ({ many }) => ({
messages: many(MessageTable),
}))
But it errors out, Error: There is not enough information to infer relation "UserTable.messages" I assume I need to tell it what relation messages is linked with, but how do I go about doing that? The docs provide no information about this.
6 Replies
Piotrek
Piotrek•16mo ago
You have to name the relations
export const MessageRelations = relations(MessageTable, ({ one }) => ({
sender: one(UserTable, {
fields: [MessageTable.sender_id],
references: [UserTable.id],
relationName: "sentMessages"
}),

recipient: one(UserTable, {
fields: [MessageTable.recipient_id],
references: [UserTable.id],
relationName: "receivedMessages"
}),
}))

export const UserRelations = relations(UserTable, ({ many }) => ({
sentMessages: many(MessageTable, { relationName: "sentMessages" }),
receivedMessages: many(MessageTable, { relationName: "receivedMessages" }),
}))
export const MessageRelations = relations(MessageTable, ({ one }) => ({
sender: one(UserTable, {
fields: [MessageTable.sender_id],
references: [UserTable.id],
relationName: "sentMessages"
}),

recipient: one(UserTable, {
fields: [MessageTable.recipient_id],
references: [UserTable.id],
relationName: "receivedMessages"
}),
}))

export const UserRelations = relations(UserTable, ({ many }) => ({
sentMessages: many(MessageTable, { relationName: "sentMessages" }),
receivedMessages: many(MessageTable, { relationName: "receivedMessages" }),
}))
@yuval59 btw. Drizzle has a very good typescript DX, you can always check if there are some fields you're missing by looking at autocompletion 😉 that's how i learned about them haha
Asseater Peter
Asseater PeterOP•16mo ago
I see, I did look at the autocomplete for the many() thing but I didn't think of doing it to the one() config, my bad I do think this stuff should probably be better explained in the docs tho, seems like a pretty obvious thing to need ¯\_(ツ)_/¯
Piotrek
Piotrek•16mo ago
yeah they should write about them in the docs i also struggled with configuring those relations but i had multiple many-to-many ones
Asseater Peter
Asseater PeterOP•16mo ago
mmmmm that sounds like fun Like, if you had multiple one-to-one relations this wouldn't be a problem (Because of the fields + references thing)
Piotrek
Piotrek•16mo ago
yeah, i had to use the relationName in 3 places and debug where it should be lol
Asseater Peter
Asseater PeterOP•16mo ago
Anyways, thank you very much for the help! It works, so I'm gonna close the thread
Want results from more Discord servers?
Add your server