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):
Postgres1.28.3prisma CLI: prisma/1.28.3 (linux-x64) node-v10.15.3Debian Linux 9.8I 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!
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:
Output of current version is the same as described by the @domusofsail