question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Optional relation using a required field makes model unusable

See original GitHub issue

Bug 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:open
  • Created 2 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
jhnnscommented, Nov 30, 2021

You are trying to modify the ids directly here so you can’t use connect or other similar inputs and you will need to directly set the foreign key.

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 about foo_id.

0reactions
janpiocommented, Nov 29, 2021

What would the correct connect syntax be here @pantharshit00?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why required and optional is removed in Protocol Buffers 3
If you are already using a proto, you can't add a required field because old application's won't be providing that field and applications...
Read more >
Visio Database Model - child has parent optional check box
I need child has parent Checkbox Optional to be unchecked. ... Relationship Type = Non-identifying - Migrate the key using FK not PK....
Read more >
How to configure the fields that appear on records
For many record fields, you can do the following: Mark the field as required (or not required); Hide the field (or show it);...
Read more >
Salesforce Error: Required Fields are Missing
This error means that either a mapping was missing in a step of your connector, the field was missing in the response, or...
Read more >
Considerations for Relationships - Salesforce Help
You can convert a master-detail relationship to a lookup relationship as long as no roll-up summary fields exist on the master object. Converting...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found