Struggling to establish a many-to-many relationship in the Drizzle ORM schema using a PostgreSQL junction table. Despite following advice from other users to add relation names on both sides of the relation, I'm still encountering an error stating "Error: There is not enough information to infer relation 'TemplateDefinitions.colorSets'". Most examples I found were for one-to-many relationships, not many-to-many like mine.
Even after adding relation names to both sides of the main relation and attempting to define the relations in the junction table itself, I faced challenges. Drizzle pointed out multiple relations with the same name when I tried to do so.
In summary, my schema involves templateDefinitions having many colorSets, and colorSets having many templateDefinitions. I've included the relevant section below with the addition of relation names.
Update: Adding relation names to all other many-to-many relationships resulted in similar errors. However, queries using "with" on one-to-many relationships worked perfectly fine.
export const TemplateDefinitions = pgTable('template_definitions', {
id: uuid('id').primaryKey().defaultRandom(),
name: varchar('name', { length: 256 }).notNull(),
})
// More schema details
The issue surfaces when I launch Drizzle Studio or attempt to run a query in my code similar to the one below:
const templateDefinition = await db.query.TemplateDefinitions.findFirst({
where: eq(TemplateDefinitions.id, id),
with: {
colorSets: {
with: {
colorSet: true,
},
},
},
})