Skip to content

NullConstraintViolation #166

Closed
Closed

Description

@iherger

I have the following models (simplified for reporting the issue):

model User {
  id         String        @default(cuid()) @id @unique
  email      String        @unique
  staff      Staff?
}

model Staff {
  id        String   @default(cuid()) @id @unique
  user      User
  active    Boolean?
}

When trying to add a user with the following query

mutation {
  createOneUser (data: {
    email: "[email protected]"
  }) {
    id
    email
  }
}

I get the following error

"error": "ConnectorError(NullConstraintViolation { field_name: \"User.staff\" })"

This seems odd, since the staff field in the User model is optional. I have other 1-1 relations with the same problem.

Activity

  1. pantharshit00 commented on Jul 9, 2019

    @pantharshit00
    Contributor

    Ok, this is partly related to #82.

    In this case what lift is creating a foreign key in the User table. Since the relation is not optional from both ends, it ends up throwing a NullConstraint violation error even though our API technically allows null relations between user and staff. So either mark the relation optional from both ends or what you can do is to force the creation of the foreign key in the Staff table instead of the User table if you don't want to make it optional from the other end.

    But I think we should automatically infer this from the datamodel and create the foreign in the table which has constraint set to required.

    You can do this using the @relation directive as shown in the example below.

    datasource db {
      provider = "sqlite"
      url      = "file:./dev.db"
    }
    
    generator photon {
      provider = "photonjs"
    }
    
    generator nexus_prisma {
      provider = "nexus-prisma"
    }
    
    model Staff {
      id     String   @default(cuid()) @id @unique
      user   User     @relation(references: [id])
      active Boolean?
    }
    
    model User {
      id    String @default(cuid()) @id @unique
      email String @unique
      staff Staff?
    }

    I think we might close this as a duplicate of #82

  2. removed their assignment
    on Jul 9, 2019
  3. iherger commented on Jul 10, 2019

    @iherger
    Author

    Thank you very much. I want to have the relation optional from the User end (not every User is Staff), but mandatory on the Staff end (every Staff member is also a User), so I changed the data model (using the @relation(references: [id]) directive as you indicated.

    Creation of a user no works fine, however, deletion doesn't work (for a user which doesn't have a related Staff record)...

    mutation {
      deleteOneUser (where: {id: "cjxx1fw8200028q9kl32u972i"}) {
        id
      }
    }

    throws the error

    ConnectorError(QueryError(Error { kind: Db, cause: Some(DbError { severity: \"ERROR\", parsed_severity: Some(Error), code: SqlState(\"42703\"), message: \"column User.staff does not exist\"
    
  4. pantharshit00 commented on Jul 10, 2019

    @pantharshit00
    Contributor

    Can you please confirm that you have deleted and recreated the database before trying this?

  5. iherger commented on Jul 10, 2019

    @iherger
    Author

    Yes, I can confirm. This is on a completely new database.

  6. iherger commented on Jul 10, 2019

    @iherger
    Author

    And on a related note: when I try to update a User to add a related Staff record, using the following mutation

    mutation {
      updateOneUser (where: {id:"cjxx1fw8200028q9kl32u972i" }, data: { staff: { create: { active: true } } } ) {
        id
        firstname
      }
    }

    I get the following error:

    ConnectorError(QueryError(Error { kind: Db, cause: Some(DbError { severity: \"ERROR\", parsed_severity: Some(Error), code: SqlState(\"42703\"), message: \"column User.staff does not exist\"
    

    (I am trying to migrate my Prisma1 resolvers to Prisma2, so all these things come up...)

  7. iherger commented on Jul 10, 2019

    @iherger
    Author

    To recapitulate (after some more tests with [email protected], no nexus actually, pure photonjs):

    • this is about a 1-1 relation with a required relation in one direction (every Staff has to be User) and a an optional relation in the other direction (a User can be Staff, but doesn't have to be)
    • attaching the relation to the Staff model helps with the initial create issue and update also works
    • delete throws an error, column User.staff does not exist
    • create User with a nested create of a related Staff record fails with NullConstraintViolation { field_name: "Staff.cjxxkzbco0001509kf7jbwtaa, t, null)."
    • update User with a nested create of a related Staff record fails with column User.staff does not exist

    I hope this helps.

  8. pantharshit00 commented on Jul 15, 2019

    @pantharshit00
    Contributor

    Thanks, I can confirm all of this. Most of them related to the query engine so I am going to transfer this issue.

  9. added
    bug/2-confirmed
    kind/bug
    on Jul 15, 2019
  10. pantharshit00 commented on Aug 5, 2019

    @pantharshit00
    Contributor

    I can no longer reproduce any of these conditions now with [email protected], binary version: 29f6d9f7f6d0a85cc30df1809733612bab56a1c5

    These have been fixed between the updates

  11. added 2 commits that reference this issue on Apr 14, 2020
    7775bee
    6137eb8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bug/2-confirmedkind/bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions