Warm tip: This article is reproduced from stackoverflow.com, please click
postgresql prisma prisma-graphql

Prisma2 prisma introspect returning weird values for foreign keys

发布于 2020-07-16 23:46:10

I have two tables, User and Relationship. The tables are used to store a parent child relationship. I am using Postgres

// schema.sql

CREATE TABLE "public"."Relationships" (
    id SERIAL PRIMARY KEY NOT NULL,
    parent_id INT NOT NULL,
    FOREIGN KEY (parent_id) REFERENCES "User" (id),
    child_id INT NOT NULL,
    FOREIGN KEY (child_id) REFERENCES "User" (id)
)

CREATE TABLE "public"."User" (
    id SERIAL PRIMARY KEY NOT NULL,
    name VARCHAR(128) NOT NULL,
    email VARCHAR(128) UNIQUE,
    password VARCHAR(128) NOT NULL,
    isChild BOOLEAN NOT NULL DEFAULT false
    created_at TIMESTAMP NOT NULL DEFAULT NOW();
);  

When I run npx prisma introspect the following is returned in the schema.prisma file.

// schema.prisma

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

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

model Relationships {
  child_id                           Int
  id                                 Int  @default(autoincrement()) @id
  parent_id                          Int
  User_Relationships_child_idToUser  User @relation("Relationships_child_idToUser", fields: [child_id], references: [id])
  User_Relationships_parent_idToUser User @relation("Relationships_parent_idToUser", fields: [parent_id], references: [id])
}

model User {
  created_at                                  DateTime        @default(now())
  email                                       String?         @unique
  id                                          Int             @default(autoincrement()) @id
  ischild                                     Boolean         @default(false)
  name                                        String?
  password                                    String
  Relationships_Relationships_child_idToUser  Relationships[] @relation("Relationships_child_idToUser")
  Relationships_Relationships_parent_idToUser Relationships[] @relation("Relationships_parent_idToUser")
}

I dont understand what User_Relationships_child_idToUser and User_Relationships_parent_idToUser are and why they are not just the simple syntax that appears for foreign keys in the Prisma docs tutorial.

Questioner
Tristan
Viewed
18
nburk 2020-04-24 20:26

Prisma's introspection generates two fields for each foreign key:

  • the relation scalar field (basically the direct representation of the foreign key)
  • a relation field (annotated with the @relation attribute) – these are the fields that seem to confuse you. The reason they provide is such that you can work with the relations easily in the Prisma Client API.

You can find more info about this in the docs here.

Of course User_Relationships_parent_idToUser and User_Relationships_child_idToUser are not very nice names. You can manually adjust the schema after introspection and rename the relation fields to have friendlier names, e.g:

model Relationships {
  child_id  Int
  id        Int  @default(autoincrement()) @id
  parent_id Int
  child     User @relation("Relationships_child_idToUser", fields: [child_id], references: [id])
  parent    User @relation("Relationships_parent_idToUser", fields: [parent_id], references: [id])
}