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

qe: fix implicit many-to-many relations in multiSchema context #3523

Merged
merged 1 commit into from
Dec 16, 2022
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// tags=postgres
// exclude=cockroachdb

datasource testds {
provider = "postgresql"
url = env("TEST_DATABASE_URL")
schemas = ["veggies", "roots"]
}

generator js {
provider = "prisma-client-js"
previewFeatures = ["multiSchema"]
}

model zoodles {
id Int @id
shiratakies shirataki[]
@@schema("veggies")
}

model shirataki {
id Int @id
zoodles zoodles[]
@@schema("roots")
}

// Expected Migration:
// -- CreateSchema
// CREATE SCHEMA IF NOT EXISTS "roots";
//
// -- CreateSchema
// CREATE SCHEMA IF NOT EXISTS "veggies";
//
// -- CreateTable
// CREATE TABLE "veggies"."zoodles" (
// "id" INTEGER NOT NULL,
//
// CONSTRAINT "zoodles_pkey" PRIMARY KEY ("id")
// );
//
// -- CreateTable
// CREATE TABLE "roots"."shirataki" (
// "id" INTEGER NOT NULL,
//
// CONSTRAINT "shirataki_pkey" PRIMARY KEY ("id")
// );
//
// -- CreateTable
// CREATE TABLE "roots"."_shiratakiTozoodles" (
// "A" INTEGER NOT NULL,
// "B" INTEGER NOT NULL
// );
//
// -- CreateIndex
// CREATE UNIQUE INDEX "_shiratakiTozoodles_AB_unique" ON "roots"."_shiratakiTozoodles"("A", "B");
//
// -- CreateIndex
// CREATE INDEX "_shiratakiTozoodles_B_index" ON "roots"."_shiratakiTozoodles"("B");
//
// -- AddForeignKey
// ALTER TABLE "roots"."_shiratakiTozoodles" ADD CONSTRAINT "_shiratakiTozoodles_A_fkey" FOREIGN KEY ("A") REFERENCES "roots"."shirataki"("id") ON DELETE CASCADE ON UPDATE CASCADE;
//
// -- AddForeignKey
// ALTER TABLE "roots"."_shiratakiTozoodles" ADD CONSTRAINT "_shiratakiTozoodles_B_fkey" FOREIGN KEY ("B") REFERENCES "veggies"."zoodles"("id") ON DELETE CASCADE ON UPDATE CASCADE;
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,26 @@ mod multi_schema {
schema.to_owned()
}

pub fn multi_schema_implicit_m2m() -> String {
let schema = indoc! {
r#"
model Loop {
id Int @id
fruits Fruit[]
@@schema("shapes")
}

model Fruit {
id Int @id
loops Loop[]
@@schema("objects")
}
"#
};

schema.to_owned()
}

#[connector_test(schema(multi_schema_simple), db_schemas("schema1", "schema2"))]
async fn crud_simple(runner: Runner) -> TestResult<()> {
// CREATE
Expand Down Expand Up @@ -516,4 +536,18 @@ mod multi_schema {

Ok(())
}

#[connector_test(schema(multi_schema_implicit_m2m), db_schemas("shapes", "objects"))]
async fn implicit_m2m_simple(runner: Runner) -> TestResult<()> {
let result = runner
.query(r#"mutation { createOneFruit(data: { id: 1, loops: { create: [{ id: 11 }, { id: 12 }] }}) { id loops { id } } }"#)
.await?;
result.assert_success();
let result = result.to_string();
assert_eq!(
result,
"{\"data\":{\"createOneFruit\":{\"id\":1,\"loops\":[{\"id\":11},{\"id\":12}]}}}"
);
Ok(())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,11 @@ impl AsTable for Relation {
// table, so MSSQL can convert the `INSERT .. ON CONFLICT IGNORE` into
// a `MERGE` statement.
RelationLinkManifestation::RelationTable(ref m) => {
let db = self.model_a().internal_data_model().db_name.clone();
let table: Table = (db, m.table.clone()).into();
let model_a = self.model_a();
let prefix = model_a
.schema_name()
.unwrap_or_else(|| model_a.internal_data_model().db_name.clone());
let table: Table = (prefix, m.table.clone()).into();

table.add_unique_index(vec![Column::from("A"), Column::from("B")])
}
Expand Down