Skip to content
This repository has been archived by the owner on Jan 14, 2021. It is now read-only.

Type Generation : returned payload type on upsert not using include #660

Closed
squirly opened this issue Apr 20, 2020 · 2 comments
Closed

Type Generation : returned payload type on upsert not using include #660

squirly opened this issue Apr 20, 2020 · 2 comments
Assignees
Labels
bug/2-confirmed We have confirmed that this is a bug. kind/regression A reported bug in functionality that used to work before. tech/typescript Issue for tech TypeScript.
Milestone

Comments

@squirly
Copy link

squirly commented Apr 20, 2020

Breaking commit: prisma/prisma@4cee929#diff-55d3b6703244dc96a4a668b570e5c70d

^^ Issue is caused by the above commit, related to prisma/prisma#615. There is not explanation given as to why this change was made. @timsuchanek could you clarify this change?

Bug description

The include option only expands the result type when the query options passed to the Prisma Client extend the findMany options.

The following line decides when includes will expand the return type:
https://github.com/prisma/prisma/blob/c86af74877d9f39936b18276b3c1db8d5ff7b310/src/packages/client/src/generation/TSClient.ts#L553

How to reproduce

Add includes to an upsert, or findOne that uses a where with a composite unique lookup, that is not available in {...}WhereInput.

Using the blog fixture example:

model Like {
  id  String  @default(cuid()) @id

  userId  String
  user    User    @relation(fields: [userId], references: [id])

  postId  String
  post    Post    @relation(fields: [postId], references: [id])

  @@unique([userId, postId])
}
  const like = await prisma.like.upsert({
    where: {
      userId_postId: {
        userId: userId,
        postId: post.id,
      }
    },
    create: {
      post: {connect: {id: post.id}},
      user: {connect: {id: userId}},
    },
    update: {},
    include: {post: true},
  });

  const p = like.post;
                 |--|
                 ^ TS2339: Property 'post' does not exist on type 'Like'.

typeof like is Like despite the includes.

Expected behavior

The type correctly has the fields specified in includes.

In the above example, typeof like should be Like & {post: Post}.

Prisma information

Example in https://github.com/squirly/prisma/tree/bug-example/src/packages/client/fixtures/blog

Environment & setup

  • OS: Mac OS & Arch
  • Database: PostgreSQL
  • Prisma version:
    • Broken in: 2.0.0-beta.2
    • Works in 2.0.0-beta.1
  • Node.js version: 12.16.1
@pantharshit00
Copy link
Contributor

pantharshit00 commented Apr 21, 2020

First of all, thanks for the detailed report, really appreciate that!

Breaking commit: 4cee929#diff-55d3b6703244dc96a4a668b570e5c70d
^^ Issue is caused by the above commit, related to prisma/prisma#615. There is not explanation given as to why this change was made. @timsuchanek could you clarify this change?

This actually meant to refer #615 (of prisma-client-js repository). We recently moved code from prisma-client-js to prisma repository and unfortunately, that information is now incorrect.

I can confirm this issue. We need to fix this.

@pantharshit00 pantharshit00 transferred this issue from prisma/prisma Apr 21, 2020
@pantharshit00 pantharshit00 added bug/2-confirmed We have confirmed that this is a bug. kind/regression A reported bug in functionality that used to work before. process/candidate Candidate for next Milestone. labels Apr 21, 2020
@janpio janpio added this to the Beta 4 milestone Apr 21, 2020
@janpio janpio added tech/typescript Issue for tech TypeScript. and removed process/candidate Candidate for next Milestone. labels Apr 21, 2020
@timsuchanek timsuchanek self-assigned this Apr 22, 2020
@timsuchanek
Copy link
Contributor

Thanks for reporting @squirly.
As mentioned in the commit (prisma/prisma@4cee929#diff-0cc91727f49088eeb56266cf1b886c81R131) the fix I introduced there was about the nested findMany with orderBy case.
Your PR would have broken that case again.
Instead, I now added a type union, which is executed in the type inference by 2 separate statements. (Does it extend the first type? If not, check if it extends the second type).

This issue is fixed in the latest alpha version of @prisma/cli.
You can try it out with npm i -g @prisma/cli@alpha.

In case it’s not fixed for you - please let us know and we’ll reopen this issue!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug/2-confirmed We have confirmed that this is a bug. kind/regression A reported bug in functionality that used to work before. tech/typescript Issue for tech TypeScript.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants