prisma: Introspection fails to generate `@relation` for many-to-many relation in MySQL
Newer reproduction here: https://github.com/prisma/prisma/issues/14668#issuecomment-1755468605
Bug description
Introspection (db pull
) fails to generate @relation
for relations between tables, that are using compound keys.
How to reproduce
- Create schema from SQL bellow
- run
npx prisma db pull
- See error in relation between tables
Expected behavior
The resulting schema should create @relation
with the correct fields.
Generated:
@relation(fields: [offerID], references: [offerID], onDelete: Cascade, onUpdate: NoAction)
Correct:
@relation(fields: [offerID, periodID, carrierID], references: [offerID, periodID, carrierID], onDelete: Cascade, onUpdate: NoAction)
Prisma information
CREATE TABLE `nabzak` (
`offerID` smallint unsigned NOT NULL,
`periodID` smallint unsigned NOT NULL,
`carrierID` int NOT NULL,
PRIMARY KEY (`offerID`, `periodID`, `carrierID`),
CONSTRAINT `nabzak_zaknos` FOREIGN KEY (`offerID`, `periodID`, `carrierID`) REFERENCES `CarrierInPeriod` (`offerID`, `periodID`, `carrierID`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=cp1250 COLLATE=cp1250_czech_cs;
CREATE TABLE `CarrierInPeriod` (
`offerID` smallint unsigned NOT NULL,
`periodID` smallint unsigned NOT NULL,
`carrierID` int NOT NULL,
PRIMARY KEY (`offerID`, `periodID`, `carrierID`)
) ENGINE=InnoDB DEFAULT CHARSET=cp1250 COLLATE=cp1250_czech_cs;
Environment & setup
- OS: Windows
- Database: MySQL
- Node.js version: v16.13.2
Prisma Version
prisma : 4.1.1
@prisma/client : 4.1.1
Current platform : windows
Query Engine (Node-API) : libquery-engine 8d8414deb360336e4698a65aa45a1fbaf1ce13d8 (at node_modules\@prisma\engines\query_engine-windows.dll.node)
Migration Engine : migration-engine-cli 8d8414deb360336e4698a65aa45a1fbaf1ce13d8 (at node_modules\@prisma\engines\migration-engine-windows.exe)
Introspection Engine : introspection-core 8d8414deb360336e4698a65aa45a1fbaf1ce13d8 (at node_modules\@prisma\engines\introspection-engine-windows.exe)
Format Binary : prisma-fmt 8d8414deb360336e4698a65aa45a1fbaf1ce13d8 (at node_modules\@prisma\engines\prisma-fmt-windows.exe)
Default Engines Hash : 8d8414deb360336e4698a65aa45a1fbaf1ce13d8
Studio : 0.469.0
Preview Features : interactiveTransactions
Extracted from https://github.com/prisma/prisma/issues/14648#issuecomment-1205224365
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 45 (45 by maintainers)
To be precise, MySQL InnoDB allows foreign keys that point to a non-unique column. This makes the foreign key behave a bit like a many-to-many relation, but breaks other expected functionality of foreign keys like referential actions or
UPDATE
. We suggest you do not use this kind of foreign key, but either make the column unique or use a real many-to-many relation.TLDR for everyone with the same issue:
MySQL
InnoDB
(and a few others) allow the creation of many-to-many relations. Prisma does not support that, and adding it would prove very difficult. Prisma will not solve this as it is an edge case. DB with this relation will produce invalid schema when pulled (prisma db pull
).To anyone reading this: If you have the issue, give this comment a like, and if more than 10 are present, write a message; maybe then something could be done about it.