🌐
Prisma
prisma.io › home › models › models › models › models
Models | Prisma Documentation
Fields of type Unsupported don't appear in the generated Prisma Client API, but you can still use raw database access to query them. The MongoDB connector doesn't support Unsupported types because it supports all scalar types. Attributes modify the behavior of fields or model blocks. The following example includes three field attributes (@id , @default , and @unique ) and one block attribute (@@unique):
Discussions

database - Prisma schema unique - Stack Overflow
I want the user to have many addresses address belongs to one user and that's what i presented here But .. I want the user to have only one address that (isDefault = true) And unlimited addresses... More on stackoverflow.com
🌐 stackoverflow.com
Conditional uniqueness constraints; Partial/Filtered (Unique) Indexes
Problem I would like to be able to define a uniqueness constraint that has conditional elements to it so that it can map to and align with a conditional unique index/constraint within the database. Suggested solution Modify the @@unique ... More on github.com
🌐 github.com
108
May 6, 2021
How To Handle Prisma Unique Constraints with a Friendly Error - Show & Tell - RedwoodJS Community
For example: model Character { ... has to have a unique name. No two characters can be named the same. If you try to add a new Character with the same name – or update an existing Character with a name that already exists – then your database and Prisma will prevent the ... More on community.redwoodjs.com
🌐 community.redwoodjs.com
1
2
October 5, 2022
sql - Why does Prisma generate an unique index instead of a unique column constraint based on "unique" on the scheme? - Stack Overflow
I know that a "unique index" is equivalent to a UNIQUE column constraint, but wouldn't that be clearer when reading the raw SQL, and avoid confusion with indexes which are created for pure More on stackoverflow.com
🌐 stackoverflow.com
🌐
Prisma
prisma.io › docs › guides › general-guides › database-workflows › unique-constraints-and-indexes
Add unique constraints & indexes to your database schema
Set up database connection pooling for serverless environments with the Prisma Data Proxy. Learn more -> ... A unique constraint is a rule that ensures that all values in a column are different.
🌐
GitHub
github.com › prisma › prisma › discussions › 24485
Unique with where condition · prisma/prisma · Discussion #24485
June 10, 2024 - model Organization { id String @id @default(uuid()) name String @unique cnpj String @unique abbreviation String @unique @db.VarChar(3) email String? createdAt DateTime @default(now()) updatedAt DateTime @default(now()) @updatedAt deletedAt DateTime? @@map("organizations") } // Add any relevant Prisma Client queries here ·
Author   prisma
🌐
Prisma
prisma.io › home › schema api › schema api › schema api
Prisma Schema API | Prisma Documentation
Defines a Prisma model . Every record of a model must be uniquely identifiable.
🌐
Stack Overflow
stackoverflow.com › questions › 76440087 › prisma-schema-unique
database - Prisma schema unique - Stack Overflow
Your schema design allows a user to have multiple addresses, but it doesn't enforce the constraint that only one of these addresses can have isDefault = true. To enforce this rule, you would need to do it at the application level in your code logic, since Prisma and most SQL databases do not provide a built-in way to enforce this kind of constraint.
🌐
GitHub
github.com › prisma › prisma › issues › 6974
Conditional uniqueness constraints; Partial/Filtered (Unique) Indexes · Issue #6974 · prisma/prisma
May 6, 2021 - Modify the @@unique modifier to include a where: { } argument that could define the condition that allows this to be unique.
Author   prisma
Find elsewhere
🌐
RedwoodJS Community
community.redwoodjs.com › get help and help others › show & tell
How To Handle Prisma Unique Constraints with a Friendly Error - Show & Tell - RedwoodJS Community
October 5, 2022 - Often, your data model will have a unique constraint to prevent duplicate records. For example: model Character { id Int @id @default(autoincrement()) name String @unique appearsIn Episode[] } A Character has to have a unique name.
🌐
Prisma
prisma.io › home › composite types › composite types › composite types › composite types
Composite types | Prisma Documentation
However, you can model your data differently to enforce unique values in a record. To do so, use Prisma ORM relations to turn the composite type into a collection.
Top answer
1 of 1
1

In short it doesn't really matter as the end result is effectively the same.

i.e. an index, just that the specifically named index rather than generated named index may be less confusing. There may also be a factor in that the name is easily determined/already named should it subsequently be utilised.

  • the specifically named being when CREATE INDEX .... is used
  • the generated named being an INDEX that is automatically created

Consider this extract from

3.6 UNIQUE constraints

A UNIQUE constraint is similar to a PRIMARY KEY constraint, except that a single table may have any number of UNIQUE constraints. For each UNIQUE constraint on the table, each row must contain a unique combination of values in the columns identified by the UNIQUE constraint. For the purposes of UNIQUE constraints, NULL values are considered distinct from all other values, including other NULLs. As with PRIMARY KEYs, a UNIQUE table-constraint clause must contain only column names — the use of expressions in an indexed-column of a UNIQUE table-constraint is not supported.

In most cases, UNIQUE and PRIMARY KEY constraints are implemented by creating a unique index in the database. (The exceptions are INTEGER PRIMARY KEY and PRIMARY KEYs on WITHOUT ROWID tables.) Hence, the following schemas are logically equivalent:

CREATE TABLE t1(a, b UNIQUE);
CREATE TABLE t1(a, b PRIMARY KEY);
CREATE TABLE t1(a, b);
CREATE UNIQUE INDEX t1b ON t1(b);
  • https://www.sqlite.org/lang_createtable.html#unique_constraints

i.e. CREATE TABLE t1(a, b UNIQUE); and CREATE UNIQUE INDEX t1b ON t1(b); are functionally the same other than the name of the first index is system generated whilst the second is named according to the given name.

Demonstration

Consider the following that uses both ways and then outputs the schema:-

CREATE TABLE User1 (
    id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
    email TEXT NOT NULL,
    name TEXT
);
CREATE UNIQUE INDEX User_email_key ON User1(email);
CREATE TABLE User2 (
    id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
    email TEXT NOT NULL UNIQUE,
    name TEXT
);
SELECT * FROM sqlite_master;

Which results in :-

As you can see the generated index has been created and named according to SQLite naming conventions.

  • unlikely to be of use, there is also no associated SQL as it has been generated automatically.
🌐
Answer Overflow
answeroverflow.com › m › 1229360371278549023
@unique on optional field - Prisma
April 15, 2024 - model Player { id String @id @default(auto()) @map("_id") @db.ObjectId steam_id String @unique ... user User? @relation(fields: [user_id], references: [id]) user_id String? @db.ObjectId @unique ...
🌐
Reddit
reddit.com › r/node › prisma: partial unique constraint dependent on column?
r/node on Reddit: Prisma: Partial Unique constraint dependent on column?
August 24, 2022 -

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
3 Color Product A
4 Color Product B

Top answer
1 of 2
2

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:

  1. stopped docker
docker-compose down -v --rmi all --remove-orphans
  1. deleted all tables from the database - probably "users" and "migration" is enough. As it seems the "migration" table keeps track of your scheema
  2. restarted docker
docker-compose up -d
  1. re-deployed prisma
prisma deploy

Hope it helps.

2 of 2
1

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/

🌐
Prisma
prisma.io › dataguide › datamodeling › correctness-constraints
Correctness and Constraints | Data Modeling | Prisma's Data Guide
In a unique constraint, only one record may have any given set of values for the constrained columns. Nullability can cause problems here, since NULL never equals anything else, up to and including NULL itself.
🌐
GitHub
github.com › prisma › prisma › discussions › 23419
how to findUnique with @@unique([xxx], name: "xxx") · prisma/prisma · Discussion #23419
@map("deleted_at") fid BigInt @db.BigInt @unique type Int @db.SmallInt username String @unique fids Fids? @relation(fields: [fid], references: [fid]) @@map("fnames") } With this updated schema, you should be able to use findUnique with the username field or fid field or even the id field. For example: const res = await prisma.fnames.findUnique({ where:{ username: 'test' } })
Author   prisma