Skip to content

Commit

Permalink
qe: fix implicit many-to-many relations in multiSchema context
Browse files Browse the repository at this point in the history
We did not add any schema prefix there.

We now add the correct one (the schema of the first model).

closes prisma/prisma#16761
  • Loading branch information
tomhoule committed Dec 16, 2022
1 parent cb66a7d commit 291b9db
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 2 deletions.
@@ -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;
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(())
}
}
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

0 comments on commit 291b9db

Please sign in to comment.