-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
removing @unique
in schema.prisma
does not generate SQL to do so
#12732
Comments
Note: This might be influenced by the column also being a foreign key. |
This comment was marked as outdated.
This comment was marked as outdated.
Ok, I'm now able to reproduce this issue, which seems to happen whenever
# docker-compose.yml
version: '3.7'
services:
mysql:
image: mysql:5.7
command: --default-authentication-plugin=mysql_native_password --skip-grant-tables
restart: always
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=tests
- MYSQL_USER=prisma
ports:
- '3306:3306'
In a different terminal:
// prisma/schema.prisma
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "mysql"
url = env("DATABASE_URL")
}
// Data model
model Post {
id Int @id @default(autoincrement())
title String
author User? @relation(fields: [authorId], references: [id])
authorId Int? @unique
}
model User {
id Int @id @default(autoincrement())
name String?
posts Post[]
}
-- prisma/migrations/20220429092455_init/migration.sql
-- CreateTable
CREATE TABLE `Post` (
`id` INTEGER NOT NULL AUTO_INCREMENT,
`title` VARCHAR(191) NOT NULL,
`authorId` INTEGER NULL,
UNIQUE INDEX `Post_authorId_key`(`authorId`),
PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
-- CreateTable
CREATE TABLE `User` (
`id` INTEGER NOT NULL AUTO_INCREMENT,
`name` VARCHAR(191) NULL,
PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
-- AddForeignKey
ALTER TABLE `Post` ADD CONSTRAINT `Post_authorId_fkey` FOREIGN KEY (`authorId`) REFERENCES `User`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;
// prisma/schema.prisma
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "mysql"
url = env("DATABASE_URL")
}
// Data model
model Post {
id Int @id @default(autoincrement())
title String
author User? @relation(fields: [authorId], references: [id])
authorId Int?
}
model User {
id Int @id @default(autoincrement())
name String?
posts Post[]
}
Environment variables loaded from .env
Prisma schema loaded from prisma/schema.prisma
Datasource "db": MySQL database "tests" at "localhost:3306"
Already in sync, no schema change or pending migration was found.
✔ Generated Prisma Client (3.13.0-integration-fix-db-pull-url-overwrites-provider-cockroachdb.8 | library) to ./../node_modules/@prisma/clie
nt in 94ms If you now open another terminal and create new users with duplicated email, you can see that the unique constraint still exists.
+--------------------+
| Tables_in_tests |
+--------------------+
| Post |
| User |
| _prisma_migrations |
+--------------------+
3 rows in set (0.00 sec)
mysql> INSERT INTO `User` (`name`) VALUES ('foo');
Query OK, 1 row affected (0.01 sec)
mysql> INSERT INTO `Post` (`title`, `authorId`) VALUES ('title-1', 1);
Query OK, 1 row affected (0.01 sec)
mysql> INSERT INTO `Post` (`title`, `authorId`) VALUES ('title-2', 1);
ERROR 1062 (23000): Duplicate entry '1' for key 'Post_authorId_key' |
Is there a workaround for this bug? |
@unique
in schema.prisma
does not generate SQL to do so
@Rodrigodd One the second |
That's weird |
Any Updates?? |
This occurs for me using MySQL 5.7
To reproduce:
ALTER TABLE foo DROP KEY 'table_name_field_name_key;
generated in the migrationsThis is a problem for me, partly because I need that field not to be unique, but also because the generated prisma client thinks that everything is fine - so the typescript allows statements that then fail due to a unique key violation.
The text was updated successfully, but these errors were encountered: