Studio not enough info to infer relation

I'm getting this error:
throw new Error(
^

Error: There is not enough information to infer relation "__public__.posts.tags"
throw new Error(
^

Error: There is not enough information to infer relation "__public__.posts.tags"
Here's my schema: https://github.com/budivoogt/bv_markdown_blog/blob/a15ec5062337460647693dd070e504e9eca54e14/src/lib/schemas/drizzleSchema.ts I believe I followed the docs carefully and ensured every table has its relations defined. The app works fine but studio doesn't launch. Any suggestions?
GitHub
bv_markdown_blog/src/lib/schemas/drizzleSchema.ts at a15ec506233746...
Personal blog with Markdown posts hosted in external database. Supabase for Postgres, Auth, Storage with Drizzle ORM, Shadcn-Svelte, Tailwind CSS. - budivoogt/bv_markdown_blog
12 Replies
Sillvva
Sillvva9mo ago
postRelations should use tagsToPosts not tags
Budi
BudiOP9mo ago
That fixed it! Good eye. And the reason why is because the tags table isn't related to posts directly, so I have to use the tagsToPosts table which defines those relationships?
Sillvva
Sillvva9mo ago
Yep. Tables are linked by reference.
markk
markk9mo ago
is an intermediate table necessary in this instance?
Sillvva
Sillvva9mo ago
For many-to-many relationships, yes
markk
markk9mo ago
Ah ok, I was trying to do something like this, need an intermediate I guess. Althoo I don't think you really should 🤔
export const chatRelations = relations(chat, ({ one, many }) => ({
user: one(users, {
fields: [chat.userId],
references: [users.id],
relationName: "userChats",
}),
message: many(message, { relationName: "chatMessages" }),
}));

export const messageRelations = relations(message, ({ one }) => ({
chat: one(chat, {
fields: [message.chatId],
references: [chat.id],
relationName: "chatMessages",
}),
}));
export const chatRelations = relations(chat, ({ one, many }) => ({
user: one(users, {
fields: [chat.userId],
references: [users.id],
relationName: "userChats",
}),
message: many(message, { relationName: "chatMessages" }),
}));

export const messageRelations = relations(message, ({ one }) => ({
chat: one(chat, {
fields: [message.chatId],
references: [chat.id],
relationName: "chatMessages",
}),
}));
Could be good to make this a bit more clear in the error message, see there is a lot of recent posts in this channel about this specific error message
markk
markk9mo ago
(sorry for hijacking this thread) I didn't think my specific example was a many-to-many situation though it's one to many right? 1 chat -> many messages
Sillvva
Sillvva9mo ago
Oh, one-to-many does not need an intermediate table. Only needs an id on the many side. In your case, it would be a chatId on the message table. And you don't need a relationName unless you have multiple relations between the same two tables.
No description
markk
markk9mo ago
oh I see, thanks, that's what I initially thought not too sure why mine is not working, seems like a bug
Sillvva
Sillvva9mo ago
Your chat-message relations look fine to me
markk
markk9mo ago
yea me too.. I've spent ages debugging. Gonna try dig into the drizzle src and see what might be causing it 99.9% sure there's nothing wrong with my definitions tho
Want results from more Discord servers?
Add your server