question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Introspection] Wrong relation type when introspecting on PostgreSQL from Prisma V1

See original GitHub issue

Bug description

I’m trying to migrate from Prisma1 to Prisma2 (brownfield) but I encounter a problem with introspection.

A relation between two of my types (User and Payout) is “translated” into a 1:m relation instead of 1:1 .

How to reproduce

Here is my SQL (Postgresql) schema created by Prisma V1 the first time where I deployed the service :

CREATE TABLE "default$default"."User"
(
    id character varying(25) COLLATE pg_catalog."default" NOT NULL,
    isActive boolean NOT NULL,
    CONSTRAINT "User_pkey" PRIMARY KEY (id)
)

CREATE TABLE "default$default"."Payout"
(
    id character varying(25) COLLATE pg_catalog."default" NOT NULL,
    user character varying(25) COLLATE pg_catalog."default",
    CONSTRAINT "Payout_pkey" PRIMARY KEY (id),
    CONSTRAINT "Payout_user_fkey" FOREIGN KEY ("user") REFERENCES "default$default"."User" (id) MATCH SIMPLE
)

I kept the most important columns only. As you can see, there is no UNIQUE attribute on User.id or Payout.id columns).

My datamodel (Prisma1) :

type User {
  id: ID! @id
  isActive: Boolean! @default(value: true)
  payout: Payout @relation(name: "UserOnPayout", onDelete: CASCADE)
}

type Payout {
  id: ID! @id
  user: User @relation(link: INLINE, name: "UserOnPayout", onDelete: SET_NULL)
}

If I Introspect the database with the prisma introspect command (Prisma1), everything works fine but when I introspect with Prisma2 (npx prisma2 introspect with the latest alpha or preview024 version), I have the following datamodel with a 1:m relation:

model User {
  id                        String                      @id
  isActive                  Boolean
  payout                    Payout[]
}

model Payout {
  id                     String       @id
  user                   User?
}

What am I doing wrong ?

Expected behavior

I was expecting the following Prisma2 schema:

model User {
  id                        String                      @id
  isActive                  Boolean
  payout                    Payout?
}

model Payout {
  id                     String       @id
  user                   User?
}

Environment & setup

  • OS: Ubuntu 18.04 LTS
  • Database: PostgreSQL in Docker (image: postgres:10.3)
  • Prisma1 version: prisma@1.34.10
  • Prisma2 version: prisma2@2.0.0-preview024
  • Node.js version: 8.16.0

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
janpiocommented, May 25, 2020

FYI: This will be tackled via a later iteration of https://github.com/prisma/prisma/issues/1937

0reactions
janpiocommented, Sep 10, 2020
Read more comments on GitHub >

github_iconTop Results From Across the Web

Introspection (PostgreSQL)
IMPORTANT: When introspecting a PostgreSQL database, you're actually introspecting a schema and not a database according to the illustrated model. Learn more ...
Read more >
What is introspection? (Reference)
Introspection has one main function: Populate your Prisma schema with a data model that reflects the current database schema. Introspect your database with ......
Read more >
Schema Incompatibilities | PostgreSQL
Because there's no indication of the CUID behavior in the database, Prisma's introspection doesn't recognize it. Workaround. As a workaround, you can manually ......
Read more >
Introspection | typescript-postgres
Once the connection is established, it introspects the database (i.e. it reads the database schema). It then translates the database schema from SQL...
Read more >
Upgrading the Prisma layer to Prisma 2 | PostgreSQL
Introspect your database (that was so far managed with Prisma 1); Use the Prisma Upgrade CLI to resolve the schema incompatibilities in the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found