You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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?
}
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\"
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
Activity
pantharshit00 commentedon Jul 9, 2019
Ok, this is partly related to #82.
In this case what lift is creating a foreign key in the
Usertable. 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 theStafftable instead of theUsertable 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
@relationdirective 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
iherger commentedon Jul 10, 2019
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)...
throws the error
pantharshit00 commentedon Jul 10, 2019
Can you please confirm that you have deleted and recreated the database before trying this?
iherger commentedon Jul 10, 2019
Yes, I can confirm. This is on a completely new database.
iherger commentedon Jul 10, 2019
And on a related note: when I try to update a User to add a related Staff record, using the following mutation
I get the following error:
(I am trying to migrate my Prisma1 resolvers to Prisma2, so all these things come up...)
iherger commentedon Jul 10, 2019
To recapitulate (after some more tests with [email protected], no nexus actually, pure photonjs):
column User.staff does not existNullConstraintViolation { field_name: "Staff.cjxxkzbco0001509kf7jbwtaa, t, null)."column User.staff does not existI hope this helps.
pantharshit00 commentedon Jul 15, 2019
Thanks, I can confirm all of this. Most of them related to the query engine so I am going to transfer this issue.
pantharshit00 commentedon Aug 5, 2019
I can no longer reproduce any of these conditions now with
[email protected], binary version: 29f6d9f7f6d0a85cc30df1809733612bab56a1c5These have been fixed between the updates
Closes #166
Closes #166