medusajs / medusa

The world's most flexible commerce platform.
https://medusajs.com
MIT License
25.22k stars 2.52k forks source link

Single "belongsTo" causes error in digital products example #8768

Closed radoslaw-sz closed 1 month ago

radoslaw-sz commented 1 month ago

I am struggling with the problem for already couple of days and after a lot of debugging, unfortunately my current suspcious that there is some kind of the bug.

I am following instruction: https://docs.medusajs.com/v2/resources/recipes/digital-products/examples/standard

But in my scenario, I would like to have only single relationship between digitalProduct and its child (in my case it is called digitalProductReference instead of media as in example). Here are my models:

const DigitalProduct = model.define("digital_product", {
  id: model.id().primaryKey(),
  name: model.text(),
  reference: model.hasOne(() => DigitalProductReference, {
    mappedBy: "digitalProduct"
  }),
  orders: model.manyToMany(() => DigitalProductOrder, {
    mappedBy: "products",
  }),
})
.cascades({
  delete: ["reference"],
})

export default DigitalProduct
const DigitalProductReference = model.define("digital_product_reference", {
  id: model.id().primaryKey(),
  type: model.enum(ReferenceType),
  licenceKey: model.text().nullable(),
  url: model.text().nullable(),
  digitalProduct: model.belongsTo(() => DigitalProduct, {
    mappedBy: "reference",
  }),
})

export default DigitalProductReference

And when I am trying to create entry in database, here is the error:

error:   insert into "public"."digital_product_reference" ("id", "type", "licence_key", "digital_product_id", "created_at", "updated_at") values ('01J62VKPNTTJH3PTP4BP26XYJS', 'licence_key', '456', '01J62VKPNV9FCCM61SS6DR8JMD', '2024-08-24T18:50:35.579Z', '2024-08-24T18:50:35.579Z', '01J62VKPNV9FCCM61SS6DR8JMD') returning "id", "created_at", "updated_at" - INSERT has more expressions than target columns
{

The example works properly with [array] of digitalProductReference (or digitalProductMedias) in mappedBy, but it fails as above in case of model.hasOne relationship.

for analysis purposes, here are the examples of changeSets which are going into database: working example:

ChangeSet<DigitalProductReference> {
    entity: DigitalProductReference {
      digital_product_id: '01J62V9GN6R8KNQG75PV57X5QZ',
      id: '01J62V9GNMB00QRAR08P5SESMH',
      digitalProduct: [(DigitalProduct)],
      created_at: 2024-08-24T18:45:01.748Z,
      updated_at: 2024-08-24T18:45:01.748Z
    },
    type: 'create',
    payload: {
      id: '01J62V9GNMB00QRAR08P5SESMH',
      digital_product_id: '01J62V9GN6R8KNQG75PV57X5QZ',
      created_at: 2024-08-24T18:45:01.748Z,
      updated_at: 2024-08-24T18:45:01.748Z
    },
    name: 'DigitalProductReference',
    rootName: 'DigitalProductReference',
    collection: 'digital_product_reference',
    schema: 'public',
    originalEntity: undefined
  }

not working example:

ChangeSet<DigitalProductReference> {
    entity: DigitalProductReference {
      type: 'licence_key',
      licenceKey: '456',
      id: '01J62VKPNTTJH3PTP4BP26XYJS',
      digitalProduct: [DigitalProduct],
      digital_product_id: '01J62VKPNV9FCCM61SS6DR8JMD',
      created_at: 2024-08-24T18:50:35.579Z,
      updated_at: 2024-08-24T18:50:35.579Z
    },
    type: 'create',
    payload: {
      id: '01J62VKPNTTJH3PTP4BP26XYJS',
      type: 'licence_key',
      licenceKey: '456',
      digitalProduct: '01J62VKPNV9FCCM61SS6DR8JMD',
      created_at: 2024-08-24T18:50:35.579Z,
      updated_at: 2024-08-24T18:50:35.579Z,
      digital_product_id: '01J62VKPNV9FCCM61SS6DR8JMD'
    },
    name: 'DigitalProductReference',
    rootName: 'DigitalProductReference',
    collection: 'digital_product_reference',
    schema: 'public',
    originalEntity: undefined
  }

As you can see, in not working example, there is additional field digitalProduct in payload which probably causes errors as there are more columns than in database.

shahednasser commented 1 month ago

Hello! Can you share the code snippet that's causing the error to occur?

radoslaw-sz commented 1 month ago

hi @shahednasser - thank you for your response. To be honest, I am not sure which part of my code executes it as there are these workflows, steps etc. To reproduce this issue, it is enough to just follow the instruction here: https://docs.medusajs.com/v2/resources/recipes/digital-products/examples/standard, but instead of having hasMany just go with hasOne in digitalProduct.

shahednasser commented 1 month ago

Can you share the point in the recipe where you started running into this error? Also can you share the full error message?

radoslaw-sz commented 1 month ago

Regarding recipe - when we have creation of data models here: https://docs.medusajs.com/v2/resources/recipes/digital-products/examples/standard#create-data-models it is enough to change

  medias: model.hasMany(() => DigitalProductMedia, {
    mappedBy: "digitalProduct",
  }),

to

```ts
  medias: model.hasOne(() => DigitalProductMedia, {
    mappedBy: "digitalProduct",
  }),

This of course causes that in a lot of places the update is needed, but after alignments, it is impossible to add digital product via UI.

Here is the full error (I just change digital_product_medias to digital_product_reference as mentioned in the first message):

error:   insert into "public"."digital_product_reference" ("id", "type", "licence_key", "digital_product_id", "created_at", "updated_at") values ('01J67TCZJTPXFGM22Y9VFWEXJ3', 'licence_key', 'LICENCE_KEY-123', '01J67TCZJTPQB9YWGTSZ73VP1Q', '2024-08-26T17:05:38.907Z', '2024-08-26T17:05:38.907Z', '01J67TCZJTPQB9YWGTSZ73VP1Q') returning "id", "created_at", "updated_at" - INSERT has more expressions than target columns
{
  message: `insert into "public"."digital_product_reference" ("id", "type", "licence_key", "digital_product_id", "created_at", "updated_at") values ('01J67TCZJTPXFGM22Y9VFWEXJ3', 'licence_key', 'LICENCE_KEY-123', '01J67TCZJTPQB9YWGTSZ73VP1Q', '2024-08-26T17:05:38.907Z', '2024-08-26T17:05:38.907Z', '01J67TCZJTPQB9YWGTSZ73VP1Q') returning "id", "created_at", "updated_at" - INSERT has more expressions than target columns`,
  name: 'SyntaxErrorException',
  stack: `SyntaxErrorException: insert into "public"."digital_product_reference" ("id", "type", "licence_key", "digital_product_id", "created_at", "updated_at") values ('01J67TCZJTPXFGM22Y9VFWEXJ3', 'licence_key', 'LICENCE_KEY-123', '01J67TCZJTPQB9YWGTSZ73VP1Q', '2024-08-26T17:05:38.907Z', '2024-08-26T17:05:38.907Z', '01J67TCZJTPQB9YWGTSZ73VP1Q') returning "id", "created_at", "updated_at" - INSERT has more expressions than target columns\n` +
    '    at PostgreSqlExceptionConverter.convertException (/home/ubuntu/rsc/medusa/app-medusa/node_modules/@mikro-orm/postgresql/PostgreSqlExceptionConverter.js:32:24)\n' +
    '    at PostgreSqlDriver.convertException (/home/ubuntu/rsc/medusa/app-medusa/node_modules/@mikro-orm/core/drivers/DatabaseDriver.js:201:54)\n' +
    '    at /home/ubuntu/rsc/medusa/app-medusa/node_modules/@mikro-orm/core/drivers/DatabaseDriver.js:205:24\n' +
    '    at processTicksAndRejections (node:internal/process/task_queues:95:5)\n' +
    '    at async PostgreSqlDriver.nativeInsertMany (/home/ubuntu/rsc/medusa/app-medusa/node_modules/@mikro-orm/knex/AbstractSqlDriver.js:302:21)\n' +
    '    at async ChangeSetPersister.persistNewEntity (/home/ubuntu/rsc/medusa/app-medusa/node_modules/@mikro-orm/core/unit-of-work/ChangeSetPersister.js:88:21)\n' +
    '    at async ChangeSetPersister.executeInserts (/home/ubuntu/rsc/medusa/app-medusa/node_modules/@mikro-orm/core/unit-of-work/ChangeSetPersister.js:32:13)\n' +
    '    at async ChangeSetPersister.runForEachSchema (/home/ubuntu/rsc/medusa/app-medusa/node_modules/@mikro-orm/core/unit-of-work/ChangeSetPersister.js:71:13)\n' +
    '    at async UnitOfWork.commitCreateChangeSets (/home/ubuntu/rsc/medusa/app-medusa/node_modules/@mikro-orm/core/unit-of-work/UnitOfWork.js:741:9)\n' +
    '    at async UnitOfWork.persistToDatabase (/home/ubuntu/rsc/medusa/app-medusa/node_modules/@mikro-orm/core/unit-of-work/UnitOfWork.js:705:13)\n' +
    '    at async UnitOfWork.doCommit (/home/ubuntu/rsc/medusa/app-medusa/node_modules/@mikro-orm/core/unit-of-work/UnitOfWork.js:301:17)\n' +
    '    at async UnitOfWork.commit (/home/ubuntu/rsc/medusa/app-medusa/node_modules/@mikro-orm/core/unit-of-work/UnitOfWork.js:268:13)\n' +
    '    at async SqlEntityManager.flush (/home/ubuntu/rsc/medusa/app-medusa/node_modules/@mikro-orm/core/EntityManager.js:1122:9)\n' +
    '    at async fork.getConnection.transactional (/home/ubuntu/rsc/medusa/app-medusa/node_modules/@mikro-orm/core/EntityManager.js:775:17)\n' +
    '    at async PostgreSqlConnection.transactional (/home/ubuntu/rsc/medusa/app-medusa/node_modules/@mikro-orm/knex/AbstractSqlConnection.js:36:25)\n' +
    '    at async transactionWrapper (/home/ubuntu/rsc/medusa/app-medusa/node_modules/@medusajs/utils/src/dal/utils.ts:32:10)\n' +
    '    at async MikroOrmAbstractBaseRepository_.transaction (/home/ubuntu/rsc/medusa/app-medusa/node_modules/@medusajs/utils/src/dal/mikro-orm/mikro-orm-repository.ts:74:12)\n' +
    '    at async AbstractService_.descriptor.value (/home/ubuntu/rsc/medusa/app-medusa/node_modules/@medusajs/utils/src/modules-sdk/decorators/inject-transaction-manager.ts:44:14)\n' +
    '    at async DigitalProductModuleService.methodImplementation (/home/ubuntu/rsc/medusa/app-medusa/node_modules/@medusajs/utils/src/modules-sdk/medusa-service.ts:194:26)\n' +
    '    at async Proxy.<anonymous> (/home/ubuntu/rsc/medusa/app-medusa/node_modules/@medusajs/orchestration/src/workflow/local-workflow.ts:124:20)\n' +
    '    at async Object.async.digital_product.digital_product (/home/ubuntu/rsc/medusa/app-medusa/src/workflows/create-digital-product/steps/create-digital-product.ts:22:28)\n' +
    '    at async Object.invoke (/home/ubuntu/rsc/medusa/app-medusa/node_modules/@medusajs/workflows-sdk/src/utils/composer/helpers/create-step-handler.ts:55:52)\n' +
    '    at async DistributedTransaction.handler (/home/ubuntu/rsc/medusa/app-medusa/node_modules/@medusajs/orchestration/src/workflow/workflow-manager.ts:214:16)\n' +
    '    at async Promise.allSettled (index 0)\n' +
    '    at async promiseAll (/home/ubuntu/rsc/medusa/app-medusa/node_modules/@medusajs/utils/src/common/promise-all.ts:23:18)\n' +
    '    at async TransactionOrchestrator.executeNext (/home/ubuntu/rsc/medusa/app-medusa/node_modules/@medusajs/orchestration/src/transaction/transaction-orchestrator.ts:848:7)\n' +
    '    at async TransactionOrchestrator.resume (/home/ubuntu/rsc/medusa/app-medusa/node_modules/@medusajs/orchestration/src/transaction/transaction-orchestrator.ts:893:5)\n' +
    '    at async LocalWorkflow.run (/home/ubuntu/rsc/medusa/app-medusa/node_modules/@medusajs/orchestration/src/workflow/local-workflow.ts:365:5)\n' +
    '    at async originalExecution (/home/ubuntu/rsc/medusa/app-medusa/node_modules/@medusajs/workflows-sdk/src/helper/workflow-export.ts:102:25)\n' +
    '    at async LocalWorkflow.newRun [as run] (/home/ubuntu/rsc/medusa/app-medusa/node_modules/@medusajs/workflows-sdk/src/helper/workflow-export.ts:186:12)\n' +
    '    at async POST (/home/ubuntu/rsc/medusa/app-medusa/src/api/admin/digital-products/route.ts:58:22)\n' +
    '    at async /home/ubuntu/rsc/medusa/app-medusa/node_modules/@medusajs/utils/src/common/wrap-handler.ts:17:14\n' +
    '\n' +
    `previous error: insert into "public"."digital_product_reference" ("id", "type", "licence_key", "digital_product_id", "created_at", "updated_at") values ('01J67TCZJTPXFGM22Y9VFWEXJ3', 'licence_key', 'LICENCE_KEY-123', '01J67TCZJTPQB9YWGTSZ73VP1Q', '2024-08-26T17:05:38.907Z', '2024-08-26T17:05:38.907Z', '01J67TCZJTPQB9YWGTSZ73VP1Q') returning "id", "created_at", "updated_at" - INSERT has more expressions than target columns\n` +
    '    at Parser.parseErrorMessage (/home/ubuntu/rsc/medusa/app-medusa/node_modules/pg-protocol/src/parser.ts:369:69)\n' +
    '    at Parser.handlePacket (/home/ubuntu/rsc/medusa/app-medusa/node_modules/pg-protocol/src/parser.ts:188:21)\n' +
    '    at Parser.parse (/home/ubuntu/rsc/medusa/app-medusa/node_modules/pg-protocol/src/parser.ts:103:30)\n' +
    '    at Socket.<anonymous> (/home/ubuntu/rsc/medusa/app-medusa/node_modules/pg-protocol/src/index.ts:7:48)\n' +
    '    at Socket.emit (node:events:519:28)\n' +
    '    at Socket.emit (node:domain:488:12)\n' +
    '    at addChunk (node:internal/streams/readable:559:12)\n' +
    '    at readableAddChunkPushByteMode (node:internal/streams/readable:510:3)\n' +
    '    at Socket.Readable.push (node:internal/streams/readable:390:5)\n' +
    '    at TCP.onStreamRead (node:internal/stream_base_commons:190:23)\n' +
    '    at TCP.callbackTrampoline (node:internal/async_hooks:130:17)',
  length: 117,
  severity: 'ERROR',
  code: '42601',
  detail: undefined,
  hint: undefined,
  position: '288',
  internalPosition: undefined,
  internalQuery: undefined,
  where: undefined,
  schema: undefined,
  table: undefined,
  column: undefined,
  dataType: undefined,
  constraint: undefined,
  file: 'analyze.c',
  line: '1026',
  routine: 'transformInsertRow'
}
shahednasser commented 1 month ago

Looking at the error stack trace, the error occurs at src/workflows/create-digital-product/steps/create-digital-product.ts line 22, and the workflow is executed at src/api/admin/digital-products/route.ts:58:22

Can you share the code in both files? Also the request you're making to the create digital product API route (with the request body)?

radoslaw-sz commented 1 month ago

hi @shahednasser - it looks like it is being handled :)