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

Relation table for implicit m:n-relation not recognized by introspection #1885

Closed
nikolasburk opened this issue Mar 19, 2020 · 2 comments · Fixed by prisma/prisma-engines#595
Assignees
Labels
bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. topic: introspection
Milestone

Comments

@nikolasburk
Copy link
Member

nikolasburk commented Mar 19, 2020

Bug description

Relation table for implicit m:n-relation not recognized by introspection.

How to reproduce

I have this Prisma schema:

generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "postgresql"
  url      = "postgresql://nikolasburk:nikolasburk@localhost:5432/newdb"
}

model Category {
  id    Int    @default(autoincrement()) @id
  posts Post[]
}
model Post {
  id         Int        @default(autoincrement()) @id
  categories Category[]
}

Now I run:

prisma2 migrate save --experimental
prisma2 migrate up --experimental

This created this README.md for the migration:

# Migration `20200319192142-init`

This migration has been generated by Nikolas Burk at 3/19/2020, 7:21:42 PM.
You can check out the [state of the schema](./schema.prisma) after the migration.

## Database Steps

```sql
CREATE TABLE "public"."Category" (
    "id" SERIAL,
    PRIMARY KEY ("id")
) 

CREATE TABLE "public"."Post" (
    "id" SERIAL,
    PRIMARY KEY ("id")
) 

CREATE TABLE "public"."_CategoryToPost" (
    "A" integer  NOT NULL ,
    "B" integer  NOT NULL 
) 

CREATE UNIQUE INDEX "_CategoryToPost_AB_unique" ON "public"."_CategoryToPost"("A","B")

CREATE  INDEX "_CategoryToPost_B_index" ON "public"."_CategoryToPost"("B")

ALTER TABLE "public"."_CategoryToPost" ADD FOREIGN KEY ("A")REFERENCES "public"."Category"("id") ON DELETE CASCADE  ON UPDATE CASCADE

ALTER TABLE "public"."_CategoryToPost" ADD FOREIGN KEY ("B")REFERENCES "public"."Post"("id") ON DELETE CASCADE  ON UPDATE CASCADE
```

## Changes

```diff
diff --git schema.prisma schema.prisma
migration ..20200319192142-init
--- datamodel.dml
+++ datamodel.dml
@@ -1,0 +1,18 @@
+generator client {
+  provider = "prisma-client-js"
+}
+
+datasource db {
+  provider = "postgresql"
+  url      = "postgresql://nikolasburk:nikolasburk@localhost:5432/newdb"
+}
+
+model Category {
+  id    Int    @default(autoincrement()) @id
+  posts Post[]
+}
+
+model Post {
+  id         Int        @default(autoincrement()) @id
+  categories Category[]
+}
```

The DB looks as follows:

CREATE TABLE "_CategoryToPost" (
    "A" integer NOT NULL REFERENCES "Category"(id) ON DELETE CASCADE ON UPDATE CASCADE,
    "B" integer NOT NULL REFERENCES "Post"(id) ON DELETE CASCADE ON UPDATE CASCADE
);
CREATE UNIQUE INDEX "_CategoryToPost_AB_unique" ON "_CategoryToPost"("A" int4_ops,"B" int4_ops);
CREATE INDEX "_CategoryToPost_B_index" ON "_CategoryToPost"("B" int4_ops);
CREATE TABLE "Category" (
    id integer DEFAULT nextval('"Category_id_seq"'::regclass) PRIMARY KEY
);
CREATE UNIQUE INDEX "Category_pkey" ON "Category"(id int4_ops);
CREATE TABLE "Post" (
    id integer DEFAULT nextval('"Post_id_seq"'::regclass) PRIMARY KEY
);
CREATE UNIQUE INDEX "Post_pkey" ON "Post"(id int4_ops);

Then I remove the models from the Prisma schema and run:

prisma2 introspect

The resulting models in the Prisma schema look as follows:

model Category {
  id             Int              @default(autoincrement()) @id
  CategoryToPost CategoryToPost[] @relation("CategoryTo_CategoryToPost")
}

model Post {
  id             Int              @default(autoincrement()) @id
  CategoryToPost CategoryToPost[] @relation("PostTo_CategoryToPost")
}

model CategoryToPost {
  A Category @relation("CategoryTo_CategoryToPost")
  B Post     @relation("PostTo_CategoryToPost")

  @@index([B], name: "_CategoryToPost_B_index")
  @@map("_CategoryToPost")
  @@unique([A, B], name: "_CategoryToPost_AB_unique")
}

Expected behavior

I would except to obtain the same models as the initial schema with the implicit m:n-relation:

model Category {
  id    Int    @default(autoincrement()) @id
  posts Post[]
}

model Post {
  id         Int        @default(autoincrement()) @id
  categories Category[]
}

Tested with alpha and preview024 with same results.

@martineboh

This comment has been minimized.

@nikolasburk

This comment has been minimized.

@do4gr do4gr self-assigned this Mar 20, 2020
@janpio janpio added this to the Preview 25 milestone Mar 20, 2020
@janpio janpio added bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. labels Mar 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. topic: introspection
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants