Skip to content

Retrieve the invalid entry that caused a foreign key constraint to be triggered #15046

Open

Description

@BryanDGuy

Note: I am only familiar with Postgres, so I will be articulating my problem and suggestions from purely that point of view. It is not my intention that we come up with custom logic for only Postgres. I just don't want to make incorrect assumptions about the other datasources that Prisma supports, so I will not be mentioning them here.

Problem

I support an application that uses Postgres as an underlying DB. Our application makes use of joining tables, with foreign key constraints to keep referential integrity in check.

Let's imagine we're on an online retailer. We sell "products" and we also sell "combos" which represent a collection of products. The schema may be represented as so:

model Combo {
  id               String          @id @default(dbgenerated("uuid_generate_v4()")  @db.Uuid
  name             String          @db.VarChar(255)
  comboProduct     ComboProduct[]
}

model ComboProduct {
  comboId      String
  productId    String
  upcharge     Int
  combo        Combo     @relation(fields: [comboId], references: [id], onDelete: Cascade, onUpdate: NoAction, map: "combo_product_combo_id_foreign")
  product      Product   @relation(fields: [productId], references: [id], onDelete: Cascade, onUpdate: NoAction, map: "combo_product_product_id_foreign")
}

model Product {
  id               String          @id @default(dbgenerated("uuid_generate_v4()")  @db.Uuid
  name             String          @db.VarChar(255)
  price            Int
  comboProduct     ComboProduct[]
}

Say a user wishes to then create a combo with a set of products in it. Our underlying Prisma query would be:

const createdCombo = await prismaClient.combo.create({
  data: {
    name,
    comboProduct: {
      createMany: {
        data: comboProductsFromUser
      }
    }
  }
})

Imagine there was a non-existent productId supplied within that comboProductsFromUser input by the client. Our API returns the non-existent entry back to the client for convenience. Currently, we catch Prisma's error object and if it failed due to the combo_product_product_id_foreign constraint being trigged, we must query our DB to find which productId doesn't exist, and then proceed to return that to the client.

As you could imagine, this is not only a performance inefficiency, but more importantly, it gets very difficult to go find what's missing in our more complicated API's. It is a drain on development & maintainability of the application.

Suggested solution

Prisma, in some way, returns this invalid entry back when throwing an error response due to a foreign key constraint. I think either could be sufficient in solving the problem, but I'm open to discussion:

  1. Prisma's error object has a rawError field, which would be the error message that was thrown by Postgres. We can read that, parse from the DETAIL field of the Postgres response (which contains the invalid entry), and return that invalid ID back to the client.
  2. Prisma's error object is enhanced to contain the failing entry.

Alternatives

We do what we have been doing so far - we lookup non-existent entries within the DB ourselves if an error is thrown.

Additional context

N/A

Activity

  1. added
    kind/feature
    topic: error reporting
    on Aug 30, 2022
  2. s0161026 commented on May 17, 2023

    @s0161026

    This feature will be super helpful for our use case so our team won't need to parse the error details to get entity information.

  3. added and removed
    topic: error reporting
    on Jul 14, 2023
  4. janpio commented on Jan 30, 2024

    @janpio
    Contributor

    Slightly related: #7254

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions