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

Re-Introspection: referentialIntegrity = prisma not respected when using @@map() #11022

Closed
Tracked by #11441
SamuelQuinones opened this issue Jan 8, 2022 · 3 comments · Fixed by prisma/prisma-engines#3335
Assignees
Labels
bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. team/schema Issue for team Schema. tech/engines Issue for tech Engines. topic: re-introspection topic: referentialIntegrity/relationMode
Milestone

Comments

@SamuelQuinones
Copy link

SamuelQuinones commented Jan 8, 2022

Bug description

I have the following schema for a database (see below)

and when i run prisma db pull all of my relations are removed.

however when i remove the use of the @@map() function call, the relations are maintained. I am unsure if this is by design or not?

I created the database before i knew what prisma was and we have a few stored procedures so updating the table names would not be without a headache

How to reproduce

  1. create a schema
  2. enable the referentialIntegrity feature and set it to prisma
  3. create a schema using maps
  4. run prisma generate
  5. run prisma db push
  6. run prisma db pull and see the relations are removed

Expected behavior

relations are maintained even when using the @@map() function

Prisma information

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

datasource db {
  provider             = "mysql"
  url                  = env("DATABASE_URL")
  referentialIntegrity = "prisma"
}

model MemberRole {
  id      Int      @id @default(autoincrement())
  name    String   @unique(map: "role_name_unique") @db.VarChar(255)
  members Member[]

  @@map("member_roles_friendly")
}

model Member {
  id                 Int        @id @default(autoincrement())
  active             Boolean    @default(true)
  name               String     @db.VarChar(50)
  photo              String?    @db.Text
  github             String?    @db.Text
  youtube            String?    @db.Text
  twitter            String?    @db.Text
  website            String?    @db.Text
  role_id            Int
  role               MemberRole @relation(fields: [role_id], references: [id])
  coreteam           Boolean    @default(false)
  joined_timestamp   DateTime?  @default(now()) @db.Timestamp(0)
  inactive_timestamp DateTime?  @db.Timestamp(0)
  modified_timestamp DateTime?  @default(now()) @db.Timestamp(0)

  @@map("members")
}

model Repository {
  id                 Int       @id @default(autoincrement())
  repo_name          String    @unique(map: "repo_name_unique") @db.VarChar(255)
  repo_link          String?   @db.Text
  modified_timestamp DateTime  @default(now()) @db.Timestamp(0)
  web_api_support    Boolean   @default(false)
  releases           Release[]

  @@map("release_repo_friendly")
}

model ReleaseResource {
  id            Int     @id @default(autoincrement())
  release_id    Int
  release       Release @relation(fields: [release_id], references: [id])
  name          String  @db.VarChar(255)
  download_link String  @db.Text

  @@map("release_resources")
}

model Release {
  id                 Int               @id @default(autoincrement())
  version            String            @db.VarChar(255)
  is_pre_release     Boolean           @default(false)
  repo_id            Int
  repo               Repository        @relation(fields: [repo_id], references: [id])
  release_page       String            @db.Text
  release_timestamp  DateTime          @default(now()) @db.Timestamp(0)
  modified_timestamp DateTime          @default(now()) @db.Timestamp(0)
  resources          ReleaseResource[]

  @@map("releases")
}

Environment & setup

  • OS: Mac OS, Windows
  • Database: MYSQL
  • Node.js version: Node v 16.13.0

Prisma Version

prisma                  : 3.7.0
@prisma/client          : 3.7.0
Current platform        : darwin-arm64
Query Engine (Node-API) : libquery-engine 8746e055198f517658c08a0c426c7eec87f5a85f (at node_modules/@prisma/engines/libquery_engine-darwin-arm64.dylib.node)
Migration Engine        : migration-engine-cli 8746e055198f517658c08a0c426c7eec87f5a85f (at node_modules/@prisma/engines/migration-engine-darwin-arm64)
Introspection Engine    : introspection-core 8746e055198f517658c08a0c426c7eec87f5a85f (at node_modules/@prisma/engines/introspection-engine-darwin-arm64)
Format Binary           : prisma-fmt 8746e055198f517658c08a0c426c7eec87f5a85f (at node_modules/@prisma/engines/prisma-fmt-darwin-arm64)
Default Engines Hash    : 8746e055198f517658c08a0c426c7eec87f5a85f
Studio                  : 0.445.0
Preview Features        : referentialIntegrity
@SamuelQuinones SamuelQuinones added the kind/bug A reported bug. label Jan 8, 2022
@janpio janpio added bug/0-unknown Bug is new, does not have information for reproduction or reproduction could not be confirmed. team/schema Issue for team Schema. topic: re-introspection topic: referentialIntegrity/relationMode labels Jan 8, 2022
@janpio
Copy link
Member

janpio commented Jan 8, 2022

I can confirm this with this more minimal schema:

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

datasource db {
  provider             = "mysql"
  url                  = env("DATABASE_URL")
  referentialIntegrity = "prisma"
}

model Foo {
  id     Int @id
  bar    Bar @relation(fields: [bar_id], references: [id])
  bar_id Int @unique

  @@map("foo_db")
}

model Bar {
  id  Int  @id
  foo Foo?
}

After Re-Introspection, we only get this output and schema:

Datasource "db": MySQL database "11022" at "g0a52uhg4117.us-east-3.psdb.cloud:3306"

Introspecting based on datasource defined in prisma\schema.prisma …

✔ Introspected 2 models and wrote them into prisma\schema.prisma in 3.33s

*** WARNING ***

These models were enriched with `@@map` information taken from the previous Prisma schema.
- Model "Foo"

Run prisma generate to generate Prisma Client.
generator client {
  provider        = "prisma-client-js"
  previewFeatures = ["referentialIntegrity"]
}

datasource db {
  provider             = "mysql"
  url                  = env("DATABASE_URL")
  referentialIntegrity = "prisma"
}

model Foo {
  id     Int @id
  bar_id Int @unique

  @@map("foo_db")
}

model Bar {
  id Int @id
}

The relation and backrelation are gone. Thanks for the report @SamuelQuinones.

@janpio janpio added bug/2-confirmed Bug has been reproduced and confirmed. and removed bug/0-unknown Bug is new, does not have information for reproduction or reproduction could not be confirmed. labels Jan 8, 2022
@janpio janpio added the tech/engines Issue for tech Engines. label Mar 24, 2022
@janpio janpio mentioned this issue Jul 1, 2022
35 tasks
@janpio janpio changed the title referentialIntegrity = prisma not respected when using @@map() Re-Introspection: referentialIntegrity = prisma not respected when using @@map() Jul 28, 2022
@tomhoule tomhoule self-assigned this Oct 5, 2022
@Jolg42 Jolg42 added this to the 4.5.0 milestone Oct 5, 2022
@janpio janpio modified the milestones: 4.5.0, 4.6.0 Oct 17, 2022
@Jolg42 Jolg42 assigned jkomyno and unassigned tomhoule Oct 20, 2022
@Jolg42
Copy link
Member

Jolg42 commented Oct 20, 2022

Internal: Since @tomhoule is busy, @jkomyno will have a look

@Jolg42
Copy link
Member

Jolg42 commented Nov 4, 2022

Note: fix will be available in next 4.6.0 release planned on Tuesday (November 8th).

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. team/schema Issue for team Schema. tech/engines Issue for tech Engines. topic: re-introspection topic: referentialIntegrity/relationMode
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants