Skip to content

Commit

Permalink
Merge pull request #573 from prisma/migration-engine/unique-on-compos…
Browse files Browse the repository at this point in the history
…ite-relation-fields

Fix @unique on composite relation fields
  • Loading branch information
tomhoule committed Mar 11, 2020
2 parents c7e1c71 + 46283ea commit a09c414
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
Expand Up @@ -142,7 +142,7 @@ impl<'a> SqlSchemaCalculator<'a> {
if f.is_unique() {
Some(sql::Index {
name: format!("{}.{}", &model.db_name(), &f.db_name()),
columns: vec![f.db_name().to_owned()],
columns: f.data_source_fields().iter().map(|f| f.name.clone()).collect(),
tpe: sql::IndexType::Unique,
})
} else {
Expand Down
29 changes: 28 additions & 1 deletion migration-engine/migration-engine-tests/tests/migrations/sql.rs
Expand Up @@ -251,7 +251,7 @@ async fn multi_field_id_as_part_of_relation_must_work(api: &TestApi) -> TestResu
Ok(())
}

#[test_each_connector(tags("sql"), log = "debug")]
#[test_each_connector(tags("sql"))]
async fn remapped_multi_field_id_as_part_of_relation_must_work(api: &TestApi) -> TestResult {
let dm = r##"
model Cat {
Expand All @@ -278,3 +278,30 @@ async fn remapped_multi_field_id_as_part_of_relation_must_work(api: &TestApi) ->

Ok(())
}

#[test_each_connector(tags("sql"))]
async fn unique_constraints_on_composite_relation_fields(api: &TestApi) -> TestResult {
let dm = r##"
model Parent {
id Int @id
child Child @relation(references: [id, c]) @unique
p String
}
model Child {
id Int @id
c String
parent Parent
@@unique([id, c])
}
"##;

api.infer_apply(dm).send_assert().await?.assert_green()?;

api.assert_schema().await?.assert_table("Parent", |table| {
table.assert_index_on_columns(&["child_id", "child_c"], |idx| idx.assert_is_unique())
})?;

Ok(())
}

0 comments on commit a09c414

Please sign in to comment.