Removing `@unique` from 1:n relation scalar field not picked up by Migrate
See original GitHub issueSteps
- With the following schema, generate a migration
generator client {
provider = "prisma-client-js"
previewFeatures = ["nativeTypes"]
}
datasource db {
provider = "mysql"
url = env("DATABASE_URL")
}
model Envelope {
id Int @id @default(autoincrement())
ClaimedCoupon ClaimedCoupon[]
}
model ClaimedCoupon {
id Int @id @default(autoincrement())
envelopeId Int @unique
Envelope Envelope @relation(fields: [envelopeId], references: [id])
}
-- CreateTable
CREATE TABLE `Envelope` (
`id` INTEGER NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
-- CreateTable
CREATE TABLE `ClaimedCoupon` (
`id` INTEGER NOT NULL AUTO_INCREMENT,
`envelopeId` INTEGER NOT NULL,
UNIQUE INDEX `ClaimedCoupon.envelopeId_unique`(`envelopeId`),
PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
-- AddForeignKey
ALTER TABLE `ClaimedCoupon` ADD FOREIGN KEY (`envelopeId`) REFERENCES `Envelope`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
- Remove
@unique
from fieldenvelopeId
and run prisma migrate dev. Prisma Migrate will claim the database is in sync and not drop the index.
Notes
In general, this is a bit of an edge case, because adding the uniqueness constraint, turns the 1:n relation into a 1:1 relation. Usually, developers would utilize our specific 1:1 syntax for this.
It does impact potentially developers who may be transitioning from a 1:1 to a 1:n relation. Surely if they have defined the uniqueness constraint manually (Prisma already creates such an index under the hood).
Issue Analytics
- State:
- Created 3 years ago
- Reactions:3
- Comments:10 (7 by maintainers)
Top Results From Across the Web
GraphQL schema basics
Your GraphQL server uses a schema to describe the shape of your available data. This schema defines a hierarchy of types with fields...
Read more >Referential actions - Prisma
Referential actions let you define the update and delete behavior of related models on the database level.
Read more >Remove constraints in sequelize migration - Stack Overflow
Show activity on this post. I'm adding a unique constraint in a migration via the migrations. changeColumn function. Adding the constraint works, but...
Read more >MySQL By Examples for Beginners
A table is made up of columns (or fields) and rows (records). The SQL keywords and commands are NOT case-sensitive. For clarity, they...
Read more >Legacy SQL Functions and Operators | BigQuery - Google Cloud
Returns the exact number of non-NULL, distinct values for the specified field. FIRST(), Returns the first sequential value in the scope of the...
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
@janpio I am pretty sure this is not related to any of the relation uniqueness issues we have dealt with recently.
@twofingerrightclick There’s an issue about this: https://github.com/prisma/prisma/issues/10503 — it’s the only case where Prisma implicitly adds indexes/constraints even if they’re not in your schema, and I’d like to get rid of it, but it will be a breaking change (so it’s going to be for Prisma 4 at the earliest).