Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong typing when trying to connect models with a unique constraint on an optional field #24032

Closed
vdhpieter opened this issue Apr 30, 2024 · 3 comments
Labels
bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. kind/bug A reported bug. team/client Issue for team Client. topic: client types Types in Prisma Client topic: (@)@unique

Comments

@vdhpieter
Copy link

vdhpieter commented Apr 30, 2024

Bug description

We have a model that has an optional field that has to be unique. When trying to use the connect keyword we get a TS error that the optional field does not have the right type.

A full reproduction repo can be found here: https://github.com/vdhpieter/prisma-partial-uniqueness-bug

How to reproduce

These reproductions start from a reproduction repo after following: https://www.prisma.io/docs/getting-started/quickstart

  1. Make the optional name field unique in your schema for the User model:

https://github.com/vdhpieter/prisma-partial-uniqueness-bug/blob/c23577bc4a6050d6132f032a45b2ccb63ebd7f45/prisma/schema.prisma#L16

  1. Create an user

https://github.com/vdhpieter/prisma-partial-uniqueness-bug/blob/c23577bc4a6050d6132f032a45b2ccb63ebd7f45/script.ts#L6-L8

  1. Create a Post and connect it to the previously created user

https://github.com/vdhpieter/prisma-partial-uniqueness-bug/blob/c23577bc4a6050d6132f032a45b2ccb63ebd7f45/script.ts#L10-L13

  1. See the following TS error (also visible when running npm run start)

https://github.com/vdhpieter/prisma-partial-uniqueness-bug/blob/c23577bc4a6050d6132f032a45b2ccb63ebd7f45/script.ts#L14-L21

Expected behavior

I can use connect with models that have optional unique fields.

Prisma information

generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "sqlite"
  url      = env("DATABASE_URL")
}

model User {
  id    Int     @id @default(autoincrement())
  email String  @unique
  name  String? @unique
  posts Post[]
}

model Post {
  id        Int     @id @default(autoincrement())
  title     String
  content   String?
  published Boolean @default(false)
  author    User    @relation(fields: [authorId], references: [id])
  authorId  Int
}
  const user = await prisma.user.create({
    data: { email: "new-user@prisma.io" },
  });

  prisma.post.create({
    data: {
      title: "A Very good post",
      author: { connect: user },
    },
  });

Environment & setup

  • OS: macOS
  • Database: SQLite
  • Node.js version: 20.7.0

Prisma Version

prisma                  : 5.13.0
@prisma/client          : 5.13.0
Computed binaryTarget   : darwin-arm64
Operating System        : darwin
Architecture            : arm64
Node.js                 : v20.7.0
Query Engine (Node-API) : libquery-engine b9a39a7ee606c28e3455d0fd60e78c3ba82b1a2b (at node_modules/@prisma/engines/libquery_engine-darwin-arm64.dylib.node)
Schema Engine           : schema-engine-cli b9a39a7ee606c28e3455d0fd60e78c3ba82b1a2b (at node_modules/@prisma/engines/schema-engine-darwin-arm64)
Schema Wasm             : @prisma/prisma-schema-wasm 5.13.0-23.b9a39a7ee606c28e3455d0fd60e78c3ba82b1a2b
Default Engines Hash    : b9a39a7ee606c28e3455d0fd60e78c3ba82b1a2b
Studio                  : 0.500.0
@vdhpieter vdhpieter added the kind/bug A reported bug. label Apr 30, 2024
@SevInf SevInf added bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. topic: client types Types in Prisma Client team/client Issue for team Client. topic: (@)@unique labels May 2, 2024
@vdhpieter
Copy link
Author

@SevInf thanks for the correct labels, is there something I can do to help move this issue forward? I don't think I have the understanding of the prisma repo to solve it myself but happy to help where I can!

@SevInf
Copy link
Contributor

SevInf commented May 8, 2024

@vdhpieter I haven't actually tried running your code but from your snippet, i can make a guess:

connect field accepts only id field, not complete user object. TS probably complains exactly about that: there are extra properties in connect which it does not recognize. Try changing your code to:

prisma.post.create({
    data: {
      title: "A Very good post",
      author: { connect: { id: user.id }} },
    },
  });

@vdhpieter
Copy link
Author

@SevInf thanks for the clarification. I think I was confused because of the fact that it does work most of the time! Thanks for the explenation!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. kind/bug A reported bug. team/client Issue for team Client. topic: client types Types in Prisma Client topic: (@)@unique
Projects
None yet
Development

No branches or pull requests

2 participants