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

Schema validator is not validating optionality of relations on both ends #3747

Closed
Frozen666 opened this issue Sep 4, 2020 · 4 comments
Closed
Assignees
Labels
bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. tech/engines/datamodel Issue about parsing/validation/rendering of the Prisma schema tech/engines Issue for tech Engines. topic: prisma-client topic: schema topic: validation
Milestone

Comments

@Frozen666
Copy link

Hi Prisma Team! My Prisma Client just crashed. This is the report:

Versions

Name Version
Node v12.13.1
OS windows
Prisma 2.6.0

Error

It is possible to create an entry with an undefined create field even if the relation is required. The compiler will not complain, the entry will be created on the database, but the server will crash on .findMany

Reproduction

(using modified https://github.com/prisma/prisma-examples/tree/master/typescript/graphql)

schema.prisma:

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

datasource db {
  provider = "sqlite"
  url      = "file:./dev.db"
}

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

model Post {
  authorId  Int?
  content   String?
  id        Int     @default(autoincrement()) @id
  published Boolean @default(false)
  title     String
  author    User?   @relation(fields: [authorId], references: [id])
}

model Address {
  id     Int    @default(autoincrement()) @id
  street String
  user   User?  @relation("AddressOfUser", fields: [userId], references: [id])
  userId Int?
}

creation query:

t.field('createUserWithoutAddress', {
      type: 'User',
      args: {
        email: stringArg({ nullable: false }),
      },
      resolve: (_, { email }, ctx) => {
        return ctx.prisma.user.create({
          data: {
            email,
            address: { create: undefined },
          },
        })
      },
    })

Any user.findMany invocation will then crash afterwards. As an example the output after looking at User in Prisma Studio:

Error in request:  PrismaClientRustPanicError: PANIC: Application logic invariant error: received null value for field address which may not be null

This is a non-recoverable error which probably happens when the Prisma Query Engine has a panic.

...

If you want the Prisma team to look into it, please open the link above 🙏

    at PrismaClientFetcher.request (...\Prisma\Studio\-coding-prisma-examples-typescript-graphql\runtime\index.js:1:227975)
    at processTicksAndRejections (internal/process/task_queues.js:93:5) {
  query: 'prisma.user.findMany({\n' +
    '  take: 100,\n' +
    '  skip: 0,\n' +
    '  select: {\n' +
    '    email: true,\n' +
    '    id: true,\n' +
    '    name: true,\n' +
    '    posts: true,\n' +
    '    address: true,\n' +
    '  }\n' +
    '})'
}

Logs

  prisma-client {
  prisma-client   engineConfig: {
  prisma-client     cwd: 'C:\\coding\\prisma-examples\\typescript\\graphql\\prisma',
  prisma-client     enableDebugLogs: false,
  prisma-client     enableEngineDebugMode: undefined,
  prisma-client     datamodelPath: '...\\-coding-prisma-examples-typescript-graphql\\schema.prisma',
  prisma-client     prismaPath: undefined,
  prisma-client     engineEndpoint: undefined,
  prisma-client     generator: {
  prisma-client       binaryTargets: [],
  prisma-client       previewFeatures: [],
  prisma-client       config: [Object],
  prisma-client       name: 'photon',
  prisma-client       provider: 'prisma-client-js',
  prisma-client       output: '...\\-coding-prisma-examples-typescript-graphql',
  prisma-client       isCustomOutput: true
  prisma-client     },
  prisma-client     showColors: false,
  prisma-client     logLevel: undefined,
  prisma-client     logQueries: undefined,
  prisma-client     flags: [],
  prisma-client     clientVersion: '2.6.0',
  prisma-client     enableExperimental: [],
  prisma-client     useUds: undefined
  prisma-client   }
  prisma-client }  
@janpio
Copy link
Member

janpio commented Sep 11, 2020

Hey @Frozen666, can you share what that entry will look like in the database?

@pantharshit00
Copy link
Contributor

I can confirm this issue, looks like a validation bug to me. In the schema, the Address relation field is required on User side while it is optional on the Address side with the foreign key also being optional.

We will need to reject this schema in the validation step as it is clear invalid here.

Reproduction can be narrowed down to having a 1 record in the User table with that schema and opening Prisma studio.

@pantharshit00 pantharshit00 removed their assignment Sep 23, 2020
@pantharshit00 pantharshit00 transferred this issue from prisma/prisma-client-js Sep 23, 2020
@pantharshit00 pantharshit00 changed the title It is possible to create an entry with an undefined create field even if the relation is required Schema is not validation optionality of relations on both ends Sep 23, 2020
@pantharshit00 pantharshit00 changed the title Schema is not validation optionality of relations on both ends Schema is not validating optionality of relations on both ends Sep 23, 2020
@pantharshit00 pantharshit00 changed the title Schema is not validating optionality of relations on both ends Schema validator is not validating optionality of relations on both ends Sep 23, 2020
@janpio janpio added the tech/engines Issue for tech Engines. label Sep 30, 2020
@janpio janpio added this to the Backlog 2.9.0 milestone Sep 30, 2020
@janpio janpio modified the milestones: Backlog 2.9.0, Backlog 2.10.0 Oct 14, 2020
@janpio janpio modified the milestones: Backlog 2.10.0, Backlog 2.11.0 Oct 28, 2020
@mavilein mavilein added the tech/engines/datamodel Issue about parsing/validation/rendering of the Prisma schema label Nov 2, 2020
mavilein added a commit to prisma/prisma-engines that referenced this issue Nov 2, 2020
@mavilein
Copy link
Member

mavilein commented Nov 4, 2020

Internal Note: I don't think this is a Prisma schema validation bug. This is simply our feature for required virtual relation fields. This feature is under dicussion though. If we keep the feature the Query Engine would have to reject the given creation query as no address is provided.

@timsuchanek timsuchanek removed this from the Backlog 2.11.0 milestone Nov 11, 2020
@timsuchanek timsuchanek added this to the Backlog 2.12.0 milestone Nov 11, 2020
@matthewmueller matthewmueller modified the milestones: Backlog 2.12.0, Release 2.12.0 Nov 11, 2020
@mavilein
Copy link
Member

We decided to remove the feature for required virtual relation fields as it exposes those issues mentioned here. This is done in PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. tech/engines/datamodel Issue about parsing/validation/rendering of the Prisma schema tech/engines Issue for tech Engines. topic: prisma-client topic: schema topic: validation
Projects
None yet
Development

No branches or pull requests

6 participants