prisma / prisma-client-js

Type-safe database client for TypeScript & Node.js (ORM replacement)
Apache License 2.0
1.47k stars 67 forks source link

Id keys should be always returned for multi-field ID with a relation field #597

Closed MichalLytek closed 4 years ago

MichalLytek commented 4 years ago

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

schickling commented 4 years ago

This will be most likely addressed through this issue: https://github.com/prisma/prisma2/issues/1606#issuecomment-601753275

Also related: https://github.com/prisma/prisma2/issues/829 and https://github.com/prisma/prisma-client-js/issues/188

mavilein commented 4 years ago

This is implemented now and available on alpha.