Prisma1: Prisma introspect postgresql multiple references to the same table

Created on 13 Mar 2019  Â·  2Comments  Â·  Source: prisma/prisma1

Describe the bug
When a table has two or more foreign keys referencing the same table, generated datamodel results with duplicate fields and missing @relation directive

To Reproduce
Given this simple database schema:

create table "user" (
  id serial,
  name varchar(10),
  primary key (id)
);

create table "post" (
  id serial,
  title varchar(100),
  author int,
  coauthor int,
  foreign key (author) references "user" (id),
  foreign key (coauthor) references "user" (id),
  primary key (id)
);

Introspection produces this datamodel:

type user {
  id: Int! @unique
  name: String
  post: [post]
  post: [post]
}

type post {
  id: Int! @unique
  title: String
  author: user
  coauthor: user
}

with post field duplicated in type user

Prisma deploy results in these errors:

Deploying service `default` to stage `default` to server `local` 97ms

Errors:

  user
    ✖ The type `user` has a duplicate fieldName. The detection of duplicates is performed case insensitive. 
    ✖ The relation field `post` must specify a `@relation` directive: `@relation(name: "MyRelation")`

  post
    ✖ The relation field `author` must specify a `@relation` directive: `@relation(name: "MyRelation")`
    ✖ The relation field `coauthor` must specify a `@relation` directive: `@relation(name: "MyRelation")`

NB: missing @relation directive too

Expected behavior
I would expect something like:

type user {
  id: Int! @unique
  name: String
  posts_as_author: [post] @relation(name: "Posts_as_author")
  posts_as_coauthor: [post] @relation(name: "Posts_as_coauthor")
}

type post {
  id: Int! @unique
  title: String
  author: user @pgRelation(column: "author") @relation(name: "Posts_as_author")
  coauthor: user @pgRelation(column: "coauthor") @relation(name: "Posts_as_coauthor")
}

NB: @pgRelation is needed for Postgresql

Versions (please complete the following information):

  • Connector: Postgres
  • Prisma Server: 1.28.3
  • prisma CLI: prisma/1.28.3 (linux-x64) node-v10.15.3
  • OS: Debian Linux 9.8
bu2-confirmed areintrospection

Most helpful comment

I can confirm this. It is not even fixed by the latest introspection improvements cc @ejoebstl

the output of 1.30.0-alpha.1 simply fixes the names not the relations issue and even introduces some new issues:

type Post @pgTable(name: "post") {
  id: Int! @unique
  author: User
  coauthor: User
  title: String
}

type User @pgTable(name: "user") {
  id: Int! @unique
  name: String
  post: [Post]
  post: [Post]
}

Output of current version is the same as described by the @domusofsail

All 2 comments

I can confirm this. It is not even fixed by the latest introspection improvements cc @ejoebstl

the output of 1.30.0-alpha.1 simply fixes the names not the relations issue and even introduces some new issues:

type Post @pgTable(name: "post") {
  id: Int! @unique
  author: User
  coauthor: User
  title: String
}

type User @pgTable(name: "user") {
  id: Int! @unique
  name: String
  post: [Post]
  post: [Post]
}

Output of current version is the same as described by the @domusofsail

Adressed in #4304, thanks for the bug report!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

marktani picture marktani  Â·  3Comments

thomaswright picture thomaswright  Â·  3Comments

marktani picture marktani  Â·  3Comments

sorenbs picture sorenbs  Â·  3Comments

notrab picture notrab  Â·  3Comments