database - Prisma schema unique - Stack Overflow
Conditional uniqueness constraints; Partial/Filtered (Unique) Indexes
How To Handle Prisma Unique Constraints with a Friendly Error - Show & Tell - RedwoodJS Community
sql - Why does Prisma generate an unique index instead of a unique column constraint based on "unique" on the scheme? - Stack Overflow
Database: PostgreSQL. Not sure if this even possible in PostgreSQL world.
option_name must be unique for each product_id. A product_id cannot have duplicate option_name. Different products can have same option_name.
Options Table
| option_id (PK) | option_name | product_id (FK) |
|---|---|---|
| 1 | Color | Product A |
| 2 | Size | Product A |
|
|
|
|
| 4 | Color | Product B |
I had the same issue.
Using Windows 10 and MariaDB (XAMPP). I added also the @unique directive - I already had some users in the table - and still could add users with the same email address without any error messages in the console. However when I opened up the Docker Desktop app, I saw a buch of error messages, regarding duplicate entries (email). Re-deploying prisma seemed to be working in the terminal, but it actually was not.
The answer from @realAlexBarge put me in the right direction:
"Also be careful that the new data model is actually deployed. If you have conflicts with the existing data it might be necessary to use the force flag to overwrite the existing schema anyways if so desired."
So what I did at the end:
- stopped docker
docker-compose down -v --rmi all --remove-orphans
- deleted all tables from the database - probably "users" and "migration" is enough. As it seems the "migration" table keeps track of your scheema
- restarted docker
docker-compose up -d
- re-deployed prisma
prisma deploy
Hope it helps.
Simply append the @unique directive to the email field of the user type to mark the field as unique. E.g.:
type User {
id: ID! @id
email: String! @unique
name: String!
}
This will ensure that no two records will have the same email (other than null) as described in more detail in the prisma documentation:
Setting the unique constraint makes sure that two records of the model in question cannot have the same value for a certain field. The only exception is the null value, meaning that multiple records can have the value null without violating the constraint. Unique fields have a unique index applied in the underlying database.
See: https://www.prisma.io/docs/datamodel-and-migrations/datamodel-POSTGRES-knum/#unique
It's important however that you deploy the new data model. Only after deploying your data schema via the prisma CLI the database will consider the unique constraint for new and existing entries. Given that you configured your prisma.yml simply run:
$ prisma deploy
Also be careful that the new data model is actually deployed. If you have conflicts with the existing data it might be necessary to use the force flag to overwrite the existing schema anyways if so desired.
See: https://www.prisma.io/docs/prisma-cli-and-configuration/cli-command-reference/prisma-deploy-xcv9/
In Prisma, you can model your relations like this:
model Product {
id String @id @default(uuid())
name String
description String
sizes ProductSize[]
}
model ProductSize {
id String @id @default(uuid())
name String
description String
product Product @relation(fields: [productId], references: [id])
productId String
@@unique([productId, name])
}
This contains a unique index over a combination of two fields (productId and name). So for each product there can only be a unique named size. Example - (productId - A, Size - 1), (productId - A, Size - 2). Adding another record with (productId - A, Size - 1) would throw an error but (productId - B, Size - 1) will be allowed.
You can create unique indexes using multiple fields in postgres. https://www.postgresqltutorial.com/postgresql-indexes/postgresql-unique-index/ scroll down a bit for the multiple fields section.
Without knowing your existing table structure I can't say with confidence that this code would work but you'll want something like this.
CREATE UNIQUE INDEX idx_products_size ON products(id, size);
