medusajs / medusa

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

adding product with sales channel fails #7100

Open SGFGOV opened 2 months ago

SGFGOV commented 2 months ago

Hi

When i try to add a product along with the sales channel, it fails with the error below. If i ignore sales channel in my backend it works. But I'm unable to attach the sales channel after that also

error: null value in column "id" of relation "product_sales_channel" violates not-null constraint\n at Parser.parseErrorMessage (/node_modules/pg-protocol/dist/parser.js:287:98)\n at Parser.handlePacket (/node_modules/pg-protocol/dist/parser.js:126:29)\n at Parser.parse (/node_modules/pg-protocol/dist/parser.js:39:38)\n at Socket. (/node_modules/pg-protocol/dist/index.js:11:42)\n at Socket.emit (node:events:514:28)\n at Socket.emit (node:domain:489:12)\n at addChunk (node:internal/streams/readable:324:12)\n at readableAddChunk (node:internal/streams/readable:297:9)\n at Readable.push (node:internal/streams/readable:234:10)\n at TCP.onStreamRead (node:internal/stream_base_commons:190:23)\n at TCP.callbackTrampoline (node:internal/async_hooks:130:17) {length: 350, name: 'error', severity: 'ERROR', c…, …} code: '23502' column: 'id' constraint: undefined dataType: undefined detail: 'Failing row contains (prod_01HVTNVK32ZTD3DHMT2PHCHWWB, sc_01HAR7GSYPPQJS4RNE57J28EYD, null, 2024-04-19 07:59:17.868847+00, 2024-04-19 07:59:17.868847+00, null).' file: 'execMain.c' hint: undefined internalPosition: undefined internalQuery: undefined length: 350 line: '2003' name: 'error'

SGFGOV commented 2 months ago

A possible fix

CREATE SEQUENCE IF NOT EXISTS products_sales_channel_id_seq;

ALTER TABLE product_sales_channel ALTER COLUMN id SET DEFAULT nextval('product_sales_channel_id_seq');

CREATE OR REPLACE FUNCTION product_sales_channel_insert_trigger() RETURNS TRIGGER AS $$ BEGIN NEW.id := nextval('product_sales_channel_id_seq'); RETURN NEW; END; $$ LANGUAGE plpgsql;

CREATE TRIGGER IF NOT EXISTS product_sales_channel_insert_trigger BEFORE INSERT ON product_sales_channel FOR EACH ROW WHEN (NEW.id IS NULL) EXECUTE FUNCTION product_sales_channel_insert_trigger();

noel-chinaza commented 3 weeks ago

hello @SGFGOV, i experienced this exact issue, and i've tried and studied hard but couldn't find a work around

how did you address this?

the SEQUENCE fix didn't work for me, it failed at the ALTER TABLE statement.

SGFGOV commented 3 weeks ago

If you can afford it..youvcould just drop your dB and start clean..The issue is a compatibility between old migrations and the latest

Alternatively You could try a new dB schema

noel-chinaza commented 3 weeks ago

hello again @SGFGOV

i tried your suggestion, i dropped the database and started from 0 by running migrations. the problem has persisted still. i'm currently on medusa 1.20.6

i just disabled the sales channel LOL

thanks for your assistance