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.

Removing `@unique` from 1:n relation scalar field not picked up by Migrate

See original GitHub issue

Steps

  1. 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;

  1. Remove @unique from field envelopeId 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:open
  • Created 3 years ago
  • Reactions:3
  • Comments:10 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
tomhoulecommented, Jul 5, 2022

@janpio I am pretty sure this is not related to any of the relation uniqueness issues we have dealt with recently.

2reactions
tomhoulecommented, Dec 13, 2021

@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).

Read more comments on GitHub >

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

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