question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Remove @unique from id relationship does not remove the unique index.

See original GitHub issue

Bug 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

  1. Create a model like the below.
model Envelope {
    ClaimedCoupon    ClaimedCoupon?
}

model ClaimedCoupon {
    Envelope      Envelope  @relation(fields: [envelopeId], references: [id])
    envelopeId   @unique
}
  1. 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")
}
  1. Run create migration.
  2. No change happens.
  3. 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:closed
  • Created 3 years ago
  • Comments:16 (7 by maintainers)

github_iconTop GitHub Comments

4reactions
albertoperdomocommented, Feb 1, 2021

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-relation ClaimedCoupon? See docs

  • In 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 key envelopeId and making the back-relation ClaimedCoupon[] 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?

3reactions
tomhoulecommented, Feb 1, 2021

Ah, this index is there because you have a 1:1 relation, ti’s automatically inserted.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found