[SOLVED] "Not enough info to infer relation", many-to-many junction table w/ postgres

I have read the posts online that say you must define relation names on both sides, and I already did that, but it still isn't working. Can anyone else spot an issue here? I have a junction table representing a many-to-many relationship. The way it is organized in my schema can be seen in the following link, and the way I am trying to query it can be seen in the attached picture.
The TLDR of the schema: templateDefinition has many colorSets, colorSets have many templateDefinition. I want to query templateDefinition to include colorSets from the junction table https://gist.github.com/Connorelsea/76f12181ec4205533bdc4329970a6f66
Gist
schema.ts
GitHub Gist: instantly share code, notes, and snippets.
No description
No description
4 Replies
deviceoperator
deviceoperatorOP2mo ago
I also tried adding the relation names to the relations() for the junction table itself, but then you get an error that multiple relations have the same name. Have read a ton of threads on this problem but very few dealing with many-to-many junction tables specifically Update: I tried to triple check the schema and go ahead and add relation names on "both sides" of all many-to-many relationships. All fail with the same error, but querying using with on one-to-many relationships works. I also see quite a few tutorials with junction tables/MTM able to query using "with" without relationNames at all. Would be very interested to know when these are required and under what circumstances these are necessary and why so I can understand drizzle a bit more deeply
deviceoperator
deviceoperatorOP2mo ago
Hello, if anybody is available to give some suggestions I'd appreciate it hugely. Here is my entire schema rather than just the relevant parts, if it may help (https://gist.github.com/Connorelsea/64e72ef5b63776e79e6697e1de8cc791) also I am on the latest drizzle version. I have also tried just using with: { colorSets: true } instead of the more nested version in my original post, and I get the same infer relations error
Gist
schema.ts
GitHub Gist: instantly share code, notes, and snippets.
deviceoperator
deviceoperatorOP2mo ago
I finally got this working sort of but am still perplexed about the behavior. Originally: relations w/ one side colorSets and other side templateDefinitions, both with the same relation name in the config and both pointing to the junction table many(templateDefinitionsToColorSets) Working: relations w/ both sides called the same thing "templateDefinitionsToColorSets" and remove the relation name. If they are both the same and still have a relation name it gives the same error. Is this intended behavior? Can you not have two different names (with matching relation name config) on either side of a many-to-many relation? So you could do with { colorSets: true } instead of with { templateDefinitionsToColorSets: true } I feel like the first option is more logical/better wording when you see the shape of a returned object from an API I also feel like I've seen tutorials show the option with two different names on each side For any future readers, I finally figured it out. You can have different keys on each side of a many-to-many relation, BUT all other one-to-many relations in same relation MUST have a relation name in their config (even if they are unrelated to the junction table specifically)
deviceoperator
deviceoperatorOP2mo ago
This behavior is not consistent? Adding relationName to some one-to-many relations in other definitions breaks the inference, whereas in some it is required. Full explanation in answer here: https://stackoverflow.com/questions/79084596/how-to-avoid-not-enough-information-to-infer-relation-in-drizzle-with-many-to
Stack Overflow
How to avoid "not enough information to infer relation" in drizzle ...
I am trying to create a many-to-many relation in the Drizzle ORM schema using a PostgreSQL junction table. In other similar questions users suggest to add relation names on "both sides" o...
Want results from more Discord servers?
Add your server