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(views): ORM-454 fix panic when specifying view as either end of a m2m relation #5137

Merged
merged 3 commits into from
Jan 27, 2025
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ impl<'db> ImplicitManyToManyRelationWalker<'db> {
pub fn table_name(self) -> ImplicitManyToManyRelationTableName<'db> {
ImplicitManyToManyRelationTableName(self.relation_name())
}

/// The relation starts or ends to a view.
pub fn one_side_is_view(self) -> bool {
self.model_a().ast_model().is_view() || self.model_b().ast_model().is_view()
}
}

/// A table name for an implicit relation's join table. Useful for its Display impl.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,8 @@ fn push_relation_tables(ctx: &mut Context<'_>) {
let m2m_relations = datamodel
.db
.walk_relations()
.filter_map(|relation| relation.refine().as_many_to_many());
.filter_map(|relation| relation.refine().as_many_to_many())
.filter(|m2m| !m2m.one_side_is_view());

for m2m in m2m_relations {
let table_name = m2m.table_name().to_string();
Expand Down
27 changes: 27 additions & 0 deletions schema-engine/sql-migration-tests/tests/migrations/views.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,30 @@ fn nothing_gets_written_in_migrations(api: TestApi) {
let expected_sql = expect!["-- This is an empty migration."];
api.expect_sql_for_schema(dm, &expected_sql);
}

#[test_connector(preview_features("views"))]
fn creates_no_m2m_relations_to_views(api: TestApi) {
let dm = indoc! {r#"
generator js {
provider = "prisma-client-javascript"
previewFeatures = ["views"]
}

model Organization {
id Int @id
viewUsers ViewUser[]
}

view ViewUser {
userId Int @id @unique
organizations Organization[]
}
"#};

api.schema_push_w_datasource(dm).send().assert_green();

api.assert_schema().assert_has_no_view("ViewUser");
api.assert_schema().assert_table("Organization", |table| {
table.assert_has_column("id").assert_does_not_have_column("viewUsers")
});
}
Loading