Closed vasilemidrigan closed 10 months ago
Can I get you to share your server logs? Would help us identify the issue.
Please share your server logs of when you are attempting to delete the product category. That should tell us something about what error you are faced with :)
Please share your server logs of when you are attempting to delete the product category. That should tell us something about what error you are faced with :)
Here's the error :)
::1 - - [14/Dec/2023:10:38:53 +0000] "POST /admin/auth/token HTTP/1.1" 200 227 "-" "axios/0.24.0"
::1 - - [14/Dec/2023:10:38:53 +0000] "GET /admin/product-categories HTTP/1.1" 200 1301 "-" "axios/0.24.0"
error: new row for relation "product_category" violates check constraint "product_category_rank_check"
QueryFailedError: new row for relation "product_category" violates check constraint "product_category_rank_check"
at PostgresQueryRunner.query (/Users/vasilemidrigan/programming/projects/e-store-server/e-store/node_modules/typeorm/driver/postgres/PostgresQueryRunner.js:211:19)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async UpdateQueryBuilder.execute (/Users/vasilemidrigan/programming/projects/e-store-server/e-store/node_modules/typeorm/query-builder/UpdateQueryBuilder.js:83:33)
at async updateSubject (/Users/vasilemidrigan/programming/projects/e-store-server/e-store/node_modules/typeorm/persistence/SubjectExecutor.js:380:38)
at async Promise.all (index 0)
at async SubjectExecutor.executeUpdateOperations (/Users/vasilemidrigan/programming/projects/e-store-server/e-store/node_modules/typeorm/persistence/SubjectExecutor.js:422:9)
at async SubjectExecutor.execute (/Users/vasilemidrigan/programming/projects/e-store-server/e-store/node_modules/typeorm/persistence/SubjectExecutor.js:99:9)
at async EntityPersistExecutor.execute (/Users/vasilemidrigan/programming/projects/e-store-server/e-store/node_modules/typeorm/persistence/EntityPersistExecutor.js:140:21) {
query: 'UPDATE "public"."product_category" SET "rank" = $1, "updated_at" = CURRENT_TIMESTAMP WHERE "id" IN ($2) RETURNING "updated_at"',
parameters: [ -1, 'pcat_01HGKEE3CRZ5EGWSC20AN0E2N5' ],
driverError: error: new row for relation "product_category" violates check constraint "product_category_rank_check"
at /Users/vasilemidrigan/programming/projects/e-store-server/e-store/node_modules/pg/lib/client.js:526:17
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async PostgresQueryRunner.query (/Users/vasilemidrigan/programming/projects/e-store-server/e-store/node_modules/typeorm/driver/postgres/PostgresQueryRunner.js:178:25)
at async UpdateQueryBuilder.execute (/Users/vasilemidrigan/programming/projects/e-store-server/e-store/node_modules/typeorm/query-builder/UpdateQueryBuilder.js:83:33)
at async updateSubject (/Users/vasilemidrigan/programming/projects/e-store-server/e-store/node_modules/typeorm/persistence/SubjectExecutor.js:380:38)
at async Promise.all (index 0)
at async SubjectExecutor.executeUpdateOperations (/Users/vasilemidrigan/programming/projects/e-store-server/e-store/node_modules/typeorm/persistence/SubjectExecutor.js:422:9)
at async SubjectExecutor.execute (/Users/vasilemidrigan/programming/projects/e-store-server/e-store/node_modules/typeorm/persistence/SubjectExecutor.js:99:9)
at async EntityPersistExecutor.execute (/Users/vasilemidrigan/programming/projects/e-store-server/e-store/node_modules/typeorm/persistence/EntityPersistExecutor.js:140:21) {
length: 414,
severity: 'ERROR',
code: '23514',
detail: 'Failing row contains (pcat_01HGKEE3CRZ5EGWSC20AN0E2N5, headphones, headphones, null, pcat_01HGKEE3CRZ5EGWSC20AN0E2N5., t, f, 2023-12-01 19:42:05.718175+00, 2023-12-14 10:38:53.884851+00, -1, , null).',
hint: undefined,
position: undefined,
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: 'public',
table: 'product_category',
column: undefined,
dataType: undefined,
constraint: 'product_category_rank_check',
file: 'execMain.c',
line: '2060',
routine: 'ExecConstraints'
},
length: 414,
severity: 'ERROR',
code: '23514',
detail: 'Failing row contains (pcat_01HGKEE3CRZ5EGWSC20AN0E2N5, headphones, headphones, null, pcat_01HGKEE3CRZ5EGWSC20AN0E2N5., t, f, 2023-12-01 19:42:05.718175+00, 2023-12-14 10:38:53.884851+00, -1, , null).',
hint: undefined,
position: undefined,
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: 'public',
table: 'product_category',
column: undefined,
dataType: undefined,
constraint: 'product_category_rank_check',
file: 'execMain.c',
line: '2060',
routine: 'ExecConstraints'
}
::1 - - [14/Dec/2023:10:38:53 +0000] "DELETE /admin/product-categories/pcat_01HGKEE3CST429PXTYFNMXMTY9 HTTP/1.1" 500 86 "-" "axios/0.24.0"
error: deadlock detected
QueryFailedError: deadlock detected
at PostgresQueryRunner.query (/Users/vasilemidrigan/programming/projects/e-store-server/e-store/node_modules/typeorm/driver/postgres/PostgresQueryRunner.js:211:19)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async UpdateQueryBuilder.execute (/Users/vasilemidrigan/programming/projects/e-store-server/e-store/node_modules/typeorm/query-builder/UpdateQueryBuilder.js:83:33)
at async MaterializedPathSubjectExecutor.update (/Users/vasilemidrigan/programming/projects/e-store-server/e-store/node_modules/typeorm/persistence/tree/MaterializedPathSubjectExecutor.js:87:9)
at async updateSubject (/Users/vasilemidrigan/programming/projects/e-store-server/e-store/node_modules/typeorm/persistence/SubjectExecutor.js:358:25)
at async Promise.all (index 0)
at async SubjectExecutor.executeUpdateOperations (/Users/vasilemidrigan/programming/projects/e-store-server/e-store/node_modules/typeorm/persistence/SubjectExecutor.js:422:9)
at async SubjectExecutor.execute (/Users/vasilemidrigan/programming/projects/e-store-server/e-store/node_modules/typeorm/persistence/SubjectExecutor.js:99:9)
at async EntityPersistExecutor.execute (/Users/vasilemidrigan/programming/projects/e-store-server/e-store/node_modules/typeorm/persistence/EntityPersistExecutor.js:140:21) {
query: `UPDATE "public"."product_category" SET "mpath" = REPLACE("mpath", 'pcat_01HGKEE3CRZ5EGWSC20AN0E2N5.', 'pcat_01HGKEE3CRZ5EGWSC20AN0E2N5.'), "updated_at" = CURRENT_TIMESTAMP WHERE "mpath" LIKE $1`,
parameters: [ 'pcat_01HGKEE3CRZ5EGWSC20AN0E2N5.%' ],
driverError: error: deadlock detected
at /Users/vasilemidrigan/programming/projects/e-store-server/e-store/node_modules/pg/lib/client.js:526:17
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async PostgresQueryRunner.query (/Users/vasilemidrigan/programming/projects/e-store-server/e-store/node_modules/typeorm/driver/postgres/PostgresQueryRunner.js:178:25)
at async UpdateQueryBuilder.execute (/Users/vasilemidrigan/programming/projects/e-store-server/e-store/node_modules/typeorm/query-builder/UpdateQueryBuilder.js:83:33)
at async MaterializedPathSubjectExecutor.update (/Users/vasilemidrigan/programming/projects/e-store-server/e-store/node_modules/typeorm/persistence/tree/MaterializedPathSubjectExecutor.js:87:9)
at async updateSubject (/Users/vasilemidrigan/programming/projects/e-store-server/e-store/node_modules/typeorm/persistence/SubjectExecutor.js:358:25)
at async Promise.all (index 0)
at async SubjectExecutor.executeUpdateOperations (/Users/vasilemidrigan/programming/projects/e-store-server/e-store/node_modules/typeorm/persistence/SubjectExecutor.js:422:9)
at async SubjectExecutor.execute (/Users/vasilemidrigan/programming/projects/e-store-server/e-store/node_modules/typeorm/persistence/SubjectExecutor.js:99:9)
at async EntityPersistExecutor.execute (/Users/vasilemidrigan/programming/projects/e-store-server/e-store/node_modules/typeorm/persistence/EntityPersistExecutor.js:140:21) {
length: 333,
severity: 'ERROR',
code: '40P01',
detail: 'Process 3881 waits for ShareLock on transaction 1455; blocked by process 3882.\n' +
'Process 3882 waits for ShareLock on transaction 1454; blocked by process 3881.',
hint: 'See server log for query details.',
position: undefined,
internalPosition: undefined,
internalQuery: undefined,
where: 'while updating tuple (0,18) in relation "product_category"',
schema: undefined,
table: undefined,
column: undefined,
dataType: undefined,
constraint: undefined,
file: 'deadlock.c',
line: '1135',
routine: 'DeadLockReport'
},
length: 333,
severity: 'ERROR',
code: '40P01',
detail: 'Process 3881 waits for ShareLock on transaction 1455; blocked by process 3882.\n' +
'Process 3882 waits for ShareLock on transaction 1454; blocked by process 3881.',
hint: 'See server log for query details.',
position: undefined,
internalPosition: undefined,
internalQuery: undefined,
where: 'while updating tuple (0,18) in relation "product_category"',
schema: undefined,
table: undefined,
column: undefined,
dataType: undefined,
constraint: undefined,
file: 'deadlock.c',
line: '1135',
routine: 'DeadLockReport'
}
::1 - - [14/Dec/2023:10:38:55 +0000] "DELETE /admin/product-categories/pcat_01HGKEE3CV4G632P3BTW33PTFR HTTP/1.1" 500 86 "-" "axios/0.24.0"
error: new row for relation "product_category" violates check constraint "product_category_rank_check"
QueryFailedError: new row for relation "product_category" violates check constraint "product_category_rank_check"
at PostgresQueryRunner.query (/Users/vasilemidrigan/programming/projects/e-store-server/e-store/node_modules/typeorm/driver/postgres/PostgresQueryRunner.js:211:19)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async UpdateQueryBuilder.execute (/Users/vasilemidrigan/programming/projects/e-store-server/e-store/node_modules/typeorm/query-builder/UpdateQueryBuilder.js:83:33)
at async updateSubject (/Users/vasilemidrigan/programming/projects/e-store-server/e-store/node_modules/typeorm/persistence/SubjectExecutor.js:380:38)
at async Promise.all (index 0)
at async SubjectExecutor.executeUpdateOperations (/Users/vasilemidrigan/programming/projects/e-store-server/e-store/node_modules/typeorm/persistence/SubjectExecutor.js:422:9)
at async SubjectExecutor.execute (/Users/vasilemidrigan/programming/projects/e-store-server/e-store/node_modules/typeorm/persistence/SubjectExecutor.js:99:9)
at async EntityPersistExecutor.execute (/Users/vasilemidrigan/programming/projects/e-store-server/e-store/node_modules/typeorm/persistence/EntityPersistExecutor.js:140:21) {
query: 'UPDATE "public"."product_category" SET "rank" = $1, "updated_at" = CURRENT_TIMESTAMP WHERE "id" IN ($2) RETURNING "updated_at"',
parameters: [ -1, 'pcat_01HGKEE3CV4G632P3BTW33PTFR' ],
driverError: error: new row for relation "product_category" violates check constraint "product_category_rank_check"
at /Users/vasilemidrigan/programming/projects/e-store-server/e-store/node_modules/pg/lib/client.js:526:17
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async PostgresQueryRunner.query (/Users/vasilemidrigan/programming/projects/e-store-server/e-store/node_modules/typeorm/driver/postgres/PostgresQueryRunner.js:178:25)
at async UpdateQueryBuilder.execute (/Users/vasilemidrigan/programming/projects/e-store-server/e-store/node_modules/typeorm/query-builder/UpdateQueryBuilder.js:83:33)
at async updateSubject (/Users/vasilemidrigan/programming/projects/e-store-server/e-store/node_modules/typeorm/persistence/SubjectExecutor.js:380:38)
at async Promise.all (index 0)
at async SubjectExecutor.executeUpdateOperations (/Users/vasilemidrigan/programming/projects/e-store-server/e-store/node_modules/typeorm/persistence/SubjectExecutor.js:422:9)
at async SubjectExecutor.execute (/Users/vasilemidrigan/programming/projects/e-store-server/e-store/node_modules/typeorm/persistence/SubjectExecutor.js:99:9)
at async EntityPersistExecutor.execute (/Users/vasilemidrigan/programming/projects/e-store-server/e-store/node_modules/typeorm/persistence/EntityPersistExecutor.js:140:21) {
length: 410,
severity: 'ERROR',
code: '23514',
detail: 'Failing row contains (pcat_01HGKEE3CV4G632P3BTW33PTFR, displays, displays, null, pcat_01HGKEE3CV4G632P3BTW33PTFR., t, f, 2023-12-01 19:42:05.718914+00, 2023-12-14 10:38:53.902096+00, -1, , null).',
hint: undefined,
position: undefined,
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: 'public',
table: 'product_category',
column: undefined,
dataType: undefined,
constraint: 'product_category_rank_check',
file: 'execMain.c',
line: '2060',
routine: 'ExecConstraints'
},
length: 410,
severity: 'ERROR',
code: '23514',
detail: 'Failing row contains (pcat_01HGKEE3CV4G632P3BTW33PTFR, displays, displays, null, pcat_01HGKEE3CV4G632P3BTW33PTFR., t, f, 2023-12-01 19:42:05.718914+00, 2023-12-14 10:38:53.902096+00, -1, , null).',
hint: undefined,
position: undefined,
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: 'public',
table: 'product_category',
column: undefined,
dataType: undefined,
constraint: 'product_category_rank_check',
file: 'execMain.c',
line: '2060',
routine: 'ExecConstraints'
}
::1 - - [14/Dec/2023:10:38:55 +0000] "DELETE /admin/product-categories/pcat_01HGKEE3CRZ5EGWSC20AN0E2N5 HTTP/1.1" 500 86 "-" "axios/0.24.0"
error: new row for relation "product_category" violates check constraint "product_category_rank_check"
QueryFailedError: new row for relation "product_category" violates check constraint "product_category_rank_check"
at PostgresQueryRunner.query (/Users/vasilemidrigan/programming/projects/e-store-server/e-store/node_modules/typeorm/driver/postgres/PostgresQueryRunner.js:211:19)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async UpdateQueryBuilder.execute (/Users/vasilemidrigan/programming/projects/e-store-server/e-store/node_modules/typeorm/query-builder/UpdateQueryBuilder.js:83:33)
at async updateSubject (/Users/vasilemidrigan/programming/projects/e-store-server/e-store/node_modules/typeorm/persistence/SubjectExecutor.js:380:38)
at async Promise.all (index 0)
at async SubjectExecutor.executeUpdateOperations (/Users/vasilemidrigan/programming/projects/e-store-server/e-store/node_modules/typeorm/persistence/SubjectExecutor.js:422:9)
at async SubjectExecutor.execute (/Users/vasilemidrigan/programming/projects/e-store-server/e-store/node_modules/typeorm/persistence/SubjectExecutor.js:99:9)
at async EntityPersistExecutor.execute (/Users/vasilemidrigan/programming/projects/e-store-server/e-store/node_modules/typeorm/persistence/EntityPersistExecutor.js:140:21) {
query: 'UPDATE "public"."product_category" SET "rank" = $1, "updated_at" = CURRENT_TIMESTAMP WHERE "id" IN ($2) RETURNING "updated_at"',
parameters: [ -1, 'pcat_01HGKEE3CRZ5EGWSC20AN0E2N5' ],
driverError: error: new row for relation "product_category" violates check constraint "product_category_rank_check"
at /Users/vasilemidrigan/programming/projects/e-store-server/e-store/node_modules/pg/lib/client.js:526:17
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async PostgresQueryRunner.query (/Users/vasilemidrigan/programming/projects/e-store-server/e-store/node_modules/typeorm/driver/postgres/PostgresQueryRunner.js:178:25)
at async UpdateQueryBuilder.execute (/Users/vasilemidrigan/programming/projects/e-store-server/e-store/node_modules/typeorm/query-builder/UpdateQueryBuilder.js:83:33)
at async updateSubject (/Users/vasilemidrigan/programming/projects/e-store-server/e-store/node_modules/typeorm/persistence/SubjectExecutor.js:380:38)
at async Promise.all (index 0)
at async SubjectExecutor.executeUpdateOperations (/Users/vasilemidrigan/programming/projects/e-store-server/e-store/node_modules/typeorm/persistence/SubjectExecutor.js:422:9)
at async SubjectExecutor.execute (/Users/vasilemidrigan/programming/projects/e-store-server/e-store/node_modules/typeorm/persistence/SubjectExecutor.js:99:9)
at async EntityPersistExecutor.execute (/Users/vasilemidrigan/programming/projects/e-store-server/e-store/node_modules/typeorm/persistence/EntityPersistExecutor.js:140:21) {
length: 413,
severity: 'ERROR',
code: '23514',
detail: 'Failing row contains (pcat_01HGKEE3CRZ5EGWSC20AN0E2N5, headphones, headphones, null, pcat_01HGKEE3CRZ5EGWSC20AN0E2N5., t, f, 2023-12-01 19:42:05.718175+00, 2023-12-14 10:38:53.92442+00, -1, , null).',
hint: undefined,
position: undefined,
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: 'public',
table: 'product_category',
column: undefined,
dataType: undefined,
constraint: 'product_category_rank_check',
file: 'execMain.c',
line: '2060',
routine: 'ExecConstraints'
},
length: 413,
severity: 'ERROR',
code: '23514',
detail: 'Failing row contains (pcat_01HGKEE3CRZ5EGWSC20AN0E2N5, headphones, headphones, null, pcat_01HGKEE3CRZ5EGWSC20AN0E2N5., t, f, 2023-12-01 19:42:05.718175+00, 2023-12-14 10:38:53.92442+00, -1, , null).',
hint: undefined,
position: undefined,
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: 'public',
table: 'product_category',
column: undefined,
dataType: undefined,
constraint: 'product_category_rank_check',
file: 'execMain.c',
line: '2060',
routine: 'ExecConstraints'
}
::1 - - [14/Dec/2023:10:38:55 +0000] "DELETE /admin/product-categories/pcat_01HGKEE3CTE9EVK58GRWSCP3CT HTTP/1.1" 500 86 "-" "axios/0.24.0"
By the way, when I try to delete the category manually from admin panel, I get another error:
@riqwan, can you assist here?
@vasilemidrigan can you try this instead?
for (const category of allCategories) {
await medusa.admin.productCategories
.delete(category.id)
.then(({ id, object, deleted }) => {
console.log(`Category ${id} is deleted!`);
});
}
@vasilemidrigan can you try this instead?
for (const category of allCategories) { await medusa.admin.productCategories .delete(category.id) .then(({ id, object, deleted }) => { console.log(`Category ${id} is deleted!`); }); }
Same error:
Updated code:
export async function deleteAllCategoriesFromAPI() {
const allCategories = await medusa.admin.productCategories
.list()
.then(({ product_categories, limit, offset, count }) => {
console.log(
"There are: ",
product_categories.length,
" product categories"
);
return product_categories;
});
for (const category of allCategories) {
await medusa.admin.productCategories
.delete(category.id)
.then(({ id, object, deleted }) => {
console.log(`Category ${id} is deleted!`);
});
}
}
Thanks, can you run this on your db and show the results?
select id, name, rank, parent_category_id from product_category
. The rank might've been corrupted.
Thanks, can you run this on your db and show the results?
select id, name, rank, parent_category_id from product_category
. The rank might've been corrupted.
@vasilemidrigan you need to put a semi colon in the end in psql
select id, name, rank, parent_category_id from product_category;
@vasilemidrigan you need to put a semi colon in the end in psql
select id, name, rank, parent_category_id from product_category;
Sorry @riqwan , that's my first psql command actually :)
All good, looks like the rank was indeed corrupted.
Run this in psql first:
WITH IndexedCategories AS (
SELECT
id,
ROW_NUMBER() OVER (ORDER BY id) AS rank_index
FROM
product_category
)
UPDATE product_category
SET rank = rank_index
FROM IndexedCategories
WHERE product_category.id = IndexedCategories.id AND product_category.parent_category_id IS NULL;
and then try with the updated code:
export async function deleteAllCategoriesFromAPI() {
const allCategories = await medusa.admin.productCategories
.list()
.then(({ product_categories, limit, offset, count }) => {
console.log(
"There are: ",
product_categories.length,
" product categories"
);
return product_categories;
});
for (const category of allCategories) {
await medusa.admin.productCategories
.delete(category.id)
.then(({ id, object, deleted }) => {
console.log(`Category ${id} is deleted!`);
});
}
}
All good, looks like the rank was indeed corrupted.
Finally works! Thank you so much for help! Now the problem with corrupted ranks in my db is not an issue anymore, only if I will have another project installed, is that correct? Or you'll fix the problem so even with a new project the problem will not persist?
Categories are deleted
The new project will not have an issue.
The corruption happened because of the parallel deletes caused due to this code:
allCategories.forEach(async (category) => {
await medusa.admin.productCategories
.delete(category.id)
.then(({ id, object, deleted }) => {
console.log(`Category ${id} is deleted!`);
});
});
I'll take a look at parallel deletes later.
I'll mark this as resolved and create a ticket on my end for the parallel deletes. Thanks for surfacing this!
The new project will not have an issue.
The corruption happened because of the parallel deletes caused due to this code:
allCategories.forEach(async (category) => { await medusa.admin.productCategories .delete(category.id) .then(({ id, object, deleted }) => { console.log(`Category ${id} is deleted!`); }); });
I'll take a look at parallel deletes later.
Hm... ok. Does it means that using forEach is not recommended, and instead using your approach would be better in general when looping through arrays?
yes, at-least for now until the issue is resolved. forEach loop does not wait for all the promises in the iteration to be resolved. for loop on the other hand does.
yes, at-least for now until the issue is resolved. forEach loop does not wait for all the promises in the iteration to be resolved. for loop on the other hand does.
Ok. Thank you!
Describe the bug
Getting 500 Error when sending a request to delete a Category.
System information
Node.js version: v 20.4.0 Database: postgres://localhost/medusa-starter-default Operating system: macOS 10.15.7 Browser (if relevant): Chrome 120 on macOS (Ventura)
Screenshots
Code snippets
Additional context
I guess the problem should be specific to Categories Entities, because deleting Products work pretty fine. Getting Categories also works. The problem is with deleting them.