Prisma-client-js: Id keys should be always returned for multi-field ID with a relation field

Created on 25 Mar 2020  路  2Comments  路  Source: prisma/prisma-client-js

Problem

I have a simple Prisma schema from docs:

model Movie {
  director Director @map(["directorFirstName", "directorLastName"])
  title    String
  @@id([director, title])
}
model Director {
  firstName String
  lastName  String
  @@id([firstName, lastName])
}

When I do simple await prisma.movie.findMany() call, I receive only title property in the objects, even if the underlying query is fetching the id columns:

image

This behavior makes the object impossible to uniquely identify, which might be needed in plenty of use cases, like relations resolving in GraphQL resolvers, where we need all id columns to perform a findOne query:

  await prisma.movie.findOne({
    where: {
      director_title: {
        title: "Hello World",
        director: {
          directorFirstName: "Bob",
          directorLastName: "Nolan",
        },
      },
    },
  });

Solution

It should always return all id columns and reflect that in typings - either by new properties:

/**
 * Model Movie
 */
export type Movie = {
  title: string
  directorFirstName: string;
  directorLastName: string;
}

Or by populating the properties aka auto-select:

image

Alternatives

I'm afraid there is no alternatives available because of lack of uniquness of the response data.

For generated resolvers we can always include the director keys but this results in additional query:

prisma:query SELECT `dev`.`Director`.`firstName`, `dev`.`Director`.`lastName` FROM `dev`.`Director` WHERE (`dev`.`Director`.`firstName`,`dev`.`Director`.`lastName`) IN (VALUES (?,?),(?,?)) LIMIT ? OFFSET ?

And still breaks custom queries/resolvers 馃槥

Additional context

Most helpful comment

This is implemented now and available on alpha.

All 2 comments

This is implemented now and available on alpha.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

FluorescentHallucinogen picture FluorescentHallucinogen  路  3Comments

AhmedElywa picture AhmedElywa  路  4Comments

timsuchanek picture timsuchanek  路  4Comments

Errorname picture Errorname  路  3Comments

nikolasburk picture nikolasburk  路  3Comments