Skip to content
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

Fix prisma-engines#567 #576

Merged
merged 1 commit into from Mar 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -169,7 +169,7 @@ impl<'a> SqlSchemaCalculator<'a> {
// wants the column names.
columns: referenced_fields
.iter()
.map(|field| field.db_name().to_owned())
.flat_map(|field| field.data_source_fields().into_iter().map(|f| f.name.clone()))
.collect(),
tpe: if index_definition.tpe == IndexType::Unique {
sql::IndexType::Unique
Expand Down Expand Up @@ -266,7 +266,7 @@ impl<'a> SqlSchemaCalculator<'a> {
referenced_table: related_model.db_name().to_owned(),
referenced_columns: referenced_fields
.iter()
.map(|referenced_field| referenced_field.db_name().to_owned())
.flat_map(|field| field.data_source_fields().into_iter().map(|f| f.name.clone()))
.collect(),
on_delete_action: match column_arity(field.arity()) {
ColumnArity::Required => sql::ForeignKeyAction::Cascade,
Expand Down Expand Up @@ -313,7 +313,13 @@ impl<'a> SqlSchemaCalculator<'a> {
referenced_columns: first_unique_criterion(model_a)
.map_err(SqlError::Generic)?
.into_iter()
.map(|field| field.db_name().to_owned())
.flat_map(|field| {
field
.data_source_fields()
.into_iter()
.map(|f| f.name.clone())
.collect::<Vec<_>>()
})
.collect(),
on_delete_action: sql::ForeignKeyAction::Cascade,
},
Expand All @@ -324,7 +330,13 @@ impl<'a> SqlSchemaCalculator<'a> {
referenced_columns: first_unique_criterion(model_b)
.map_err(SqlError::Generic)?
.into_iter()
.map(|field| field.db_name().to_owned())
.flat_map(|field| {
field
.data_source_fields()
.into_iter()
.map(|f| f.name.clone())
.collect::<Vec<_>>()
})
.collect(),
on_delete_action: sql::ForeignKeyAction::Cascade,
},
Expand Down
28 changes: 28 additions & 0 deletions migration-engine/migration-engine-tests/tests/migrations/sql.rs
Expand Up @@ -305,3 +305,31 @@ async fn unique_constraints_on_composite_relation_fields(api: &TestApi) -> TestR

Ok(())
}

#[test_each_connector(tags("sql"))]
async fn indexes_on_composite_relation_fields(api: &TestApi) -> TestResult {
let dm = r##"
model User {
id Int @id
firstName String
lastName String

@@unique([firstName, lastName])
}

model SpamList {
id Int @id
user User @relation(references: [firstName, lastName])

@@index([user])
}
"##;

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

api.assert_schema().await?.assert_table("SpamList", |table| {
table.assert_index_on_columns(&["user_firstName", "user_lastName"], |idx| idx.assert_is_not_unique())
})?;

Ok(())
}