Remove @unique from id relationship does not remove the unique index.
See original GitHub issueBug description
I have a relationship between to Models. They and linked through a id. Initial I had set up that ID to be unique but the logic changed so I removed it. However, it does not remove the unique index from the MySQL table and still fails on that constraint. I had to manually remove it.
How to reproduce
- Create a model like the below.
model Envelope {
ClaimedCoupon ClaimedCoupon?
}
model ClaimedCoupon {
Envelope Envelope @relation(fields: [envelopeId], references: [id])
envelopeId @unique
}
- Then change the model to this.
model Envelope {
ClaimedCoupon ClaimedCoupon[]
}
model ClaimedCoupon {
Envelope Envelope @relation(fields: [envelopeId], references: [id])
envelopeId
}
Also tried:
model Envelope {
ClaimedCoupon ClaimedCoupon[]
}
model ClaimedCoupon {
Envelope Envelope @relation(fields: [envelopeId], references: [id])
envelopeId
@@index([envelopeId], name: "envelopeId")
}
- Run create migration.
- No change happens.
- The unique index remains.
Expected behavior
The unique index should get deleted.
Environment & setup
- OS: MacOS and Ubuntu 18
- Database: MySQL
- Node.js version: 15.3.0
- Prisma version: ^2.14.0
Issue Analytics
- State:
- Created 3 years ago
- Comments:16 (7 by maintainers)
Top Results From Across the Web
How to remove unique index from Dotnet EF? - Stack Overflow
It (randomly) selects the Suprevisor as principal and creates unique FK relationship through ManagerId . SuprevisorId is not treated as FK, ...
Read more >Delete Unique Constraints - SQL Server - Microsoft Learn
In Object Explorer, expand the table that contains the unique constraint and then expand Constraints. · Right-click the key and select Delete.
Read more >Difference between Unique Indexes and Unique Constraints ...
It provides you with an additional benefit that no one can accidentally delete the unique Index created by the unique constraint.
Read more >Remove unique index from each document - MongoDB
I have created multiple documents inside my collection that has the “name” field set to a unique index. The issue I am having...
Read more >Documentation: 15: 5.4. Constraints - PostgreSQL
PostgreSQL does not support CHECK constraints that reference table data other ... a unique constraint will automatically create a unique B-tree index on...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
OK, I re-read and did some testing again @ssmith-14Four
This is what I think is happening:
In the first schema you share you have a 1:1 relation: there is a relation scalar (
envelopeId
) and on the *opposite side there is an optional back-relationClaimedCoupon?
See docsIn this case, Prisma will create a unique index, even if none is defined in the schema. But it happens that you also have
@unique
in there. If you omit it, the resulting DB schema is the same.Then you change it to a 1:n relation, by removing the
@unique
attribute from the foreign keyenvelopeId
and making the back-relationClaimedCoupon[]
an array. At this point you would expect for the index to be dropped, but it seems you are hitting what looks like a bug that is unrelated to your previous relation syntax.I just created another issue for that here. I will add that for the 1:1 relation, prisma already adds a uniqueness constraint anyway so it’s not necessary. You can omit the
@unique
in your first schema and the result there is the same.What happens is when you transition to the second schema you want to remove your explicit @unique and this is not happening.
Does this make sense to you?
Ah, this index is there because you have a 1:1 relation, ti’s automatically inserted.