medusajs / medusa

Building blocks for digital commerce
https://medusajs.com
MIT License
23.9k stars 2.32k forks source link

update or delete on table "shipping_method" violates foreign key constraint "FK_926ca9f29014af8091722dede08" on table "shipping_method_tax_line" #7788

Open gpascualg opened 3 weeks ago

gpascualg commented 3 weeks ago

Bug report

Describe the bug

After a few failed checkouts, trying to do any modification to the cart (changing quantity or removing items) will fail. Details on how to reproduce below.

System information

Medusa version (including plugins): 1.18.0, freshly installed Node.js version: v20.14.0 Database: psql 16 Operating system: Windows Browser (if relevant): any

Steps to reproduce the behavior

To reproduce:

Since I don't have a dev env setup, to observe the error I added console.log(err) in middlewares\error-handler.js:13 See below the issue

  express:router dispatching POST /store/carts/cart_01J0VE36QRJ5JFPAZVJ7VDK87F/line-items/item_01J0VE36TSXEGJVB4E7CF7AAT7 +18s
QueryFailedError: update or delete on table "shipping_method" violates foreign key constraint "FK_926ca9f29014af8091722dede08" on table "shipping_method_tax_line"
    at PostgresQueryRunner.query (C:\Users\my-user\Documents\Github\Overlay\Store\my-store\node_modules\typeorm\driver\postgres\PostgresQueryRunner.js:219:19)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async DeleteQueryBuilder.execute (C:\Users\my-user\Documents\Github\Overlay\Store\my-store\node_modules\typeorm\query-builder\DeleteQueryBuilder.js:52:33)
    at async SubjectExecutor.executeRemoveOperations (C:\Users\my-user\Documents\Github\Overlay\Store\my-store\node_modules\typeorm\persistence\SubjectExecutor.js:461:17)
    at async SubjectExecutor.execute (C:\Users\my-user\Documents\Github\Overlay\Store\my-store\node_modules\typeorm\persistence\SubjectExecutor.js:104:9)
    at async EntityPersistExecutor.execute (C:\Users\my-user\Documents\Github\Overlay\Store\my-store\node_modules\typeorm\persistence\EntityPersistExecutor.js:140:21) {
  query: 'DELETE FROM "public"."shipping_method" WHERE "id" = $1',
  parameters: [ 'sm_01J0VEFXM0P9ZTV5MJ2WP64KPV' ],
  driverError: error: update or delete on table "shipping_method" violates foreign key constraint "FK_926ca9f29014af8091722dede08" on table "shipping_method_tax_line"
      at C:\Users\my-user\Documents\Github\Overlay\Store\my-store\node_modules\pg\lib\client.js:526:17
      at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
      at async PostgresQueryRunner.query (C:\Users\my-user\Documents\Github\Overlay\Store\my-store\node_modules\typeorm\driver\postgres\PostgresQueryRunner.js:184:25)
      at async DeleteQueryBuilder.execute (C:\Users\my-user\Documents\Github\Overlay\Store\my-store\node_modules\typeorm\query-builder\DeleteQueryBuilder.js:52:33)
      at async SubjectExecutor.executeRemoveOperations (C:\Users\my-user\Documents\Github\Overlay\Store\my-store\node_modules\typeorm\persistence\SubjectExecutor.js:461:17)
      at async SubjectExecutor.execute (C:\Users\my-user\Documents\Github\Overlay\Store\my-store\node_modules\typeorm\persistence\SubjectExecutor.js:104:9)
      at async EntityPersistExecutor.execute (C:\Users\my-user\Documents\Github\Overlay\Store\my-store\node_modules\typeorm\persistence\EntityPersistExecutor.js:140:21) {
    length: 380,
    severity: 'ERROR',
    code: '23503',
    detail: 'Key (id)=(sm_01J0VEFXM0P9ZTV5MJ2WP64KPV) is still referenced from table "shipping_method_tax_line".',
    hint: undefined,
    position: undefined,
    internalPosition: undefined,
    internalQuery: undefined,
    where: undefined,
    schema: 'public',
    table: 'shipping_method_tax_line',
    column: undefined,
    dataType: undefined,
    constraint: 'FK_926ca9f29014af8091722dede08',
    file: 'ri_triggers.c',
    line: '2633',
    routine: 'ri_ReportViolation'
  },
  length: 380,
  severity: 'ERROR',
  code: '23503',
  detail: 'Key (id)=(sm_01J0VEFXM0P9ZTV5MJ2WP64KPV) is still referenced from table "shipping_method_tax_line".',
  hint: undefined,
  position: undefined,
  internalPosition: undefined,
  internalQuery: undefined,
  where: undefined,
  schema: 'public',
  table: 'shipping_method_tax_line',
  column: undefined,
  dataType: undefined,
  constraint: 'FK_926ca9f29014af8091722dede08',
  file: 'ri_triggers.c',
  line: '2633',
  routine: 'ri_ReportViolation'
}
error:   null
Error: null
    at formatException (C:\Users\my-user\Documents\Github\Overlay\Store\my-store\node_modules\@medusajs\medusa\dist\utils\exception-formatter.js:22:24)
    at exports.default (C:\Users\my-user\Documents\Github\Overlay\Store\my-store\node_modules\@medusajs\medusa\dist\api\middlewares\error-handler.js:15:43)
    at Layer.handle_error (C:\Users\my-user\Documents\Github\Overlay\Store\my-store\node_modules\express\lib\router\layer.js:71:5)
    at trim_prefix (C:\Users\my-user\Documents\Github\Overlay\Store\my-store\node_modules\express\lib\router\index.js:326:13)
    at C:\Users\my-user\Documents\Github\Overlay\Store\my-store\node_modules\express\lib\router\index.js:286:9
    at Function.process_params (C:\Users\my-user\Documents\Github\Overlay\Store\my-store\node_modules\express\lib\router\index.js:346:12)
    at next (C:\Users\my-user\Documents\Github\Overlay\Store\my-store\node_modules\express\lib\router\index.js:280:10)
    at C:\Users\my-user\Documents\Github\Overlay\Store\my-store\node_modules\express\lib\router\index.js:646:15
    at next (C:\Users\my-user\Documents\Github\Overlay\Store\my-store\node_modules\express\lib\router\index.js:265:14)
    at C:\Users\my-user\Documents\Github\Overlay\Store\my-store\node_modules\express\lib\router\index.js:646:15 {
  type: 'not_found',
  code: undefined,
  date: 2024-06-20T21:30:04.609Z
}

Expected behavior

Item is removed / quantity is changed

gpascualg commented 3 weeks ago

Not sure this is the proper fix, but it does fix it

ALTER TABLE IF EXISTS public.shipping_method_tax_line
    DROP CONSTRAINT "FK_926ca9f29014af8091722dede08"
,   ADD CONSTRAINT "FK_926ca9f29014af8091722dede08" FOREIGN KEY (shipping_method_id)
    REFERENCES public.shipping_method (id) MATCH SIMPLE
    ON UPDATE NO ACTION
    ON DELETE CASCADE;

Currently, it is ON DELETE NO ACTION