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

Error when setting a relation field to null #459

Closed
kgoggin opened this issue Jan 30, 2020 · 5 comments
Closed

Error when setting a relation field to null #459

kgoggin opened this issue Jan 30, 2020 · 5 comments
Labels
bug/2-confirmed We have confirmed that this is a bug. kind/bug A reported bug. tech/engines Issue for tech Engines. tech/typescript Issue for tech TypeScript.
Milestone

Comments

@kgoggin
Copy link

kgoggin commented Jan 30, 2020

Hello! I just stumbled on this error that I believe to be a bug and thought I'd share it. I've got a call out to create an entity with an optional 1:1 relation to another entity. Here's the code:

ctx.prisma.lessons.create({
        data: {
          title: args.data.title,
          schoolYear: { connect: { id: schoolYear.id } },
          status: args.data.status,
          students: {
            connect: args.data.students.map(id => ({ id: id }))
          },
          subject: args.data.subjectId
            ? {
                connect: { id: args.data.subjectId }
              }
            : null,
          notes: {
            create: [
              {
                content: args.data.description,
                type: "DESCRIPTION"
              }
            ]
          },
          day
        }
      })

The offending line is where I'm setting the subject field with a ternary statement. If it evaluates as null, then the call fails with the following error:

Error occurred during query validation & transformation:\nAssertion error: Attempted conversion of non-map ParsedInputValue (Single(Null)) into map failed..

The code field is P2009 if that's helpful.

The workaround for me is to change null to undefined, which seems to work fine. Note that TypeScript is cool with either value, so either the generated types are wrong or something with the query engine is.

Thanks!

@pantharshit00
Copy link
Contributor

Thanks for reporting this! I can indeed confirm this behaviour. We should match the typing with the behaviour or we should allow nulls for connects.

Either way this is a confirmed bug now.

More minimal reproduction:

datasource db {
  provider = "sqlite" // other options are: "mysql" and "sqlite"
  url      = "sqlite:dev.db"
}

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

model User {
  id String @id @default(cuid())
  name String
  posts Post[]
}

model Post {
  id String @id @default(cuid())
  title String
  content String
  user User
}
import { PrismaClient } from "@prisma/client";

const prisma = new PrismaClient();

async function main() {
  const data = await prisma.user.create({
    data: {
      name: "Harshit Pant",
      posts: {
        connect: null
      }
    }
  });
  console.log(data);
}

main();

@pantharshit00 pantharshit00 transferred this issue from prisma/prisma Jan 30, 2020
@pantharshit00 pantharshit00 added bug/2-confirmed We have confirmed that this is a bug. kind/bug A reported bug. process/candidate Candidate for next Milestone. labels Jan 30, 2020
@janpio janpio added this to the Preview 22 milestone Jan 31, 2020
@janpio janpio removed the process/candidate Candidate for next Milestone. label Jan 31, 2020
@timsuchanek
Copy link
Contributor

Thanks a lot for reporting!
And thanks for the reproduction @pantharshit00.

I just looked into it and there is an important difference between the reported error and the reproduction.

The reported error fails in

  const data = await prisma.user.create({
    data: {
      name: "Harshit Pant",
      posts: null
    }
  });

Errors in

P2009: Failed to validate the query `Error occurred during query validation & transformation:
Assertion error: Attempted conversion of non-map ParsedInputValue (Single(Null)) into map failed..` at ``

Whereas the reproduction mentions

  const data = await prisma.user.create({
    data: {
      name: "Harshit Pant",
      posts: { connect: null }
    }
  });

And errors in

TypeError: Cannot convert undefined or null to object
    at Function.keys (<anonymous>)

They are related to different problems, but of course both need to be fixed.

@dpetrick
Copy link

Related, and probably resolved: #614

@pantharshit00
Copy link
Contributor

I can confirm this has been fixed in the latest alpha.

@kgoggin Can you please confirm, to install the alpha use npm install @prisma/cli@alpha && npx prisma generate

@janpio janpio added the tech/engines Issue for tech Engines. label Apr 21, 2020
@janpio janpio modified the milestones: Beta 3, Beta 4 Apr 21, 2020
@kgoggin
Copy link
Author

kgoggin commented Apr 21, 2020

@pantharshit00 sorry I can't easily confirm - I've since refactored that code and don't have an easy way to verify. Glad for the fix though!

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/bug A reported bug. tech/engines Issue for tech Engines. tech/typescript Issue for tech TypeScript.
Projects
None yet
Development

No branches or pull requests

8 participants