Optional relation using a required field makes model unusable
See original GitHub issueBug description
Hi there 👋,
thanks for your work ❤️. Prisma is the best ORM I’ve worked with so far 😃
Unfortunately, I’ve now encountered a situation where an optional relation creates a type that can’t be satisfied.
How to reproduce
When I have the following (simplified) data model:
model TimeEntry {
  id Int @id
  employeeId Int
  employee   Employee @relation(fields: [employeeId], references: [id])
  dayEntry DayEntry? @relation(fields: [id], references: [id])
}
it’s impossible to create a TimeEntry like this:
await prisma.timeEntry.create({
  data: { // <-- TypeScript shows an error here
    id: 2,
    employee: { connect: { id: 2 } },
  },
});
TypeScript will complain about this with the error message:
Types of property 'id' are incompatible.
  Type 'number' is not assignable to type 'undefined'
And in fact, the generated type looks like this:
  export type TimeEntryCreateInput = {
    // no id here :(
    employee: EmployeeCreateNestedOneWithoutTimeEntriesInput
    dayEntry?: DayEntryCreateNestedOneWithoutTimeEntryInput
  }
If I ignore the type error, I still get the following runtime error:
Unknown arg `id` in data.id for type TimeEntryCreateInput.
The error is caused by the optional dayEntry relation which uses the required id field. Interestingly, there’s no error anymore when I remove the employee relation. Also marking the dayEntry relation as required fixes the error.
Expected behavior
There should be no error, the data model looks reasonable to me 😃
Prisma information
Complete data model
model Employee {
  id Int @id
  timeEntries TimeEntry[]
}
model DayEntry {
  id Int @id
  timeEntry TimeEntry?
}
model TimeEntry {
  id Int @id
  employeeId Int
  employee   Employee @relation(fields: [employeeId], references: [id])
  dayEntry DayEntry? @relation(fields: [id], references: [id])
}
Call:
await prisma.timeEntry.create({
  data: { // <-- TypeScript shows an error here
    id: 2,
    employee: { connect: { id: 2 } },
  },
});
Error:
Types of property 'id' are incompatible.
  Type 'number' is not assignable to type 'undefined'
Environment & setup
- OS: Mac OS
 - Database: PostgreSQL
 - Node.js version: v16.9.1
 
Prisma Version
prisma                  : 3.2.1
@prisma/client          : 3.2.1
Current platform        : darwin
Query Engine (Node-API) : libquery-engine b71d8cb16c4ddc7e3e9821f42fd09b0f82d7934c (at node_modules/@prisma/engines/libquery_engine-darwin.dylib.node)
Migration Engine        : migration-engine-cli b71d8cb16c4ddc7e3e9821f42fd09b0f82d7934c (at node_modules/@prisma/engines/migration-engine-darwin)
Introspection Engine    : introspection-core b71d8cb16c4ddc7e3e9821f42fd09b0f82d7934c (at node_modules/@prisma/engines/introspection-engine-darwin)
Format Binary           : prisma-fmt b71d8cb16c4ddc7e3e9821f42fd09b0f82d7934c (at node_modules/@prisma/engines/prisma-fmt-darwin)
Default Engines Hash    : b71d8cb16c4ddc7e3e9821f42fd09b0f82d7934c
Studio                  : 0.435.0
Issue Analytics
- State:
 - Created 2 years ago
 - Comments:8 (5 by maintainers)
 

Top Related StackOverflow Question
I don’t understand this. I’m not trying to modify any ids, I just want to create a new entry.
bar: { connect: { bar_id: 5 }}is also not the problematic part, Prisma complains aboutfoo_id.What would the correct
connectsyntax be here @pantharshit00?