Prisma-client-js: Delete record with relations (ConstraintViolation)

Created on 21 Jul 2019  路  6Comments  路  Source: prisma/prisma-client-js

I came across a ConstraintViolation when deleting many to many relation:

ConnectorError(QueryError(SqliteFailure(Error { code: ConstraintViolation, extended_code: 787 }, Some("FOREIGN KEY constraint failed"))

i. e. I have entities linked with users, it creates table _EntityToUser.
When I delete an entity, it does not automatically delete records in EntityToUser and thus it fails.
I can disconnect the users using update, but I think it would be much more friendly if I could specify whether I want to disconnect users directly in delete

I also think, that it would be handy if photon could identify ConstraintViolations and would return the same error for all database engines

For SQLite I get this:

ConnectorError(QueryError(SqliteFailure(Error { code: ConstraintViolation, extended_code: 787 }, Some("FOREIGN KEY constraint failed"))

and for MySQL I get this:

ConnectorError(QueryError(MySqlError { ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`db`.`_EntityToUser`, CONSTRAINT `_EntityToUser_ibfk_1` FOREIGN KEY (`A`) REFERENCES `Entity` (`id`)) }

Requests:

  1. Identify ConstraintViolation errors and return the same error for every database engine
  2. Allow to specify whether to disconnect relations in delete, so there is no need to update and delete afterwards
bu1-repro-available kinbug

All 6 comments

Thanks @Rutherther. Can you share your datamodel or a subset including the relevant parts with us please?

Sure, I have made a repo for this: https://github.com/Rutherther/prisma2-many-to-many
Model:

model User {
  id    String  @default(cuid()) @id @unique
  email String  @unique
  name  String?
  entities Entity[]
}

model Entity {
  id      String  @default(cuid()) @id @unique
  users   User[]
  name    String
}

Code:

  const newEntity = await photon.entities.create({
    data: {
      name: 'Test entity',
      users: {
        connect: {
          email: '[email protected]'
        }
      }
    }
  });

  console.log(newEntity);

  await photon.entities.delete({
    where: {
      id: newEntity.id
    }
  }); // Fails

The problem is that I would have to update the entity (disconnect the users) before I delete it
I think it would be handy to add disconnect to delete, too. What do you think?

Any updates?
Running into a similar issue on preview5 when creating a one-to-one relationship
Thanks!

@JorgeCeja As a workaround you can first use update with disconnect

const entity; // ...
const users = await photon.entities.findOne({ where: { id: entity.id } }).users({ select: { id: true } });

await photon.entities.update({
  data: {
    users: {
      disconnect: users
    }
  }
});

await photon.entities.delete({ where: { id: entity.id  } });

This is an example for one-to-many relationship, but I think something like this should work for one-to-one

It would still be nice to have something like that in delete itself, too

Can you confirm the repro please @pantharshit00? Thanks.

I am no longer able to reproduce this one.

I am on version [email protected], binary version: bbbeff6f84b408e534ebd866bfd378748e6d6611

Was this page helpful?
0 / 5 - 0 ratings

Related issues

esistgut picture esistgut  路  4Comments

Vergil333 picture Vergil333  路  3Comments

julien1619 picture julien1619  路  3Comments

divyenduz picture divyenduz  路  4Comments

FluorescentHallucinogen picture FluorescentHallucinogen  路  3Comments