Skip to content

Foreign key field is not being set when using a nested create for a one-to-one relation #3081

@jwld

Description

@jwld

Bug description

Foreign key value is not being set when using a nested create for a one-to-one relation.

How to reproduce

  1. Create the following schema:
datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

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

model Company {
  id       String    @default(cuid()) @id
  payments Payment[]
}

model Visit {
  id      String   @default(cuid()) @id
  payment Payment?
}

model Payment {
  id        String  @default(cuid()) @id
  company   Company @relation(fields: [companyId], references: [id])
  companyId String
  visit     Visit?  @relation(fields: [visitId], references: [id])
  visitId   String?
}
  1. Create a company (need the id for the next step)

  2. Run the following script

const { PrismaClient } = require('@prisma/client')

const prisma = new PrismaClient()

const main = async () => {
  const newVisit = await prisma.visit.create({ data: {} })

  await prisma.visit.update({
    data: {
      payment: {
        create: {
          company: { connect: { id: '{{ company id here }}' }}
        }
      }
    },
    where: { id: newVisit.id }
  })
}

main()
  .then(() => console.log('Done 🎉'))
  .catch(err => console.log(err))
  .finally(() => prisma.disconnect())
  1. Check the database - payment.companyId will be set, however payment.visitId is still NULL, despite the payment being created within a visit update. There's no way to connect that payment to visit, since the nested create does not allow a direct visit connection (since it's part of a visit update).

Without the company connection it works, so it's perhaps something to do with connecting to multiple parents?

Expected behavior

Payments created within an update visit mutation should be connected to the visit.

Environment & setup

  • OS: Mac OS
  • Database: PostgreSQL
  • Prisma version: 2.2.2
  • Node.js version: 14.5.0

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions