Skip to content

Commit

Permalink
ie: replicate pre-datamodel-renderer single-field unique behaviour
Browse files Browse the repository at this point in the history
The first unique index defined on the field wins. This became visible in
introspection CI after
#3333 was merged.

Arguably, we should instead introspect `@@unique()`s on the model for
extra single-field uniques, but that would be a change.
  • Loading branch information
tomhoule committed Nov 1, 2022
1 parent 7ce85a7 commit 63086b6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ impl<'a> Model<'a> {
let uniques: HashMap<&str, IndexFieldOptions> = dml_model
.indices
.iter()
.rev() // replicate existing behaviour on duplicate unique constraints
.filter(|ix| ix.is_unique())
.filter(|ix| ix.defined_on_field)
.map(|ix| {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-- tags=postgres
-- exclude=cockroachdb

CREATE TABLE mymodel (
id UUID PRIMARY KEY,
thefield TEXT
);

CREATE UNIQUE INDEX unq2 ON mymodel(thefield);
CREATE UNIQUE INDEX unq3 ON mymodel(thefield);
CREATE UNIQUE INDEX unq1 ON mymodel(thefield);

/*
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = "env(TEST_DATABASE_URL)"
}
model mymodel {
id String @id @db.Uuid
thefield String? @unique(map: "unq1")
}
*/

0 comments on commit 63086b6

Please sign in to comment.