Skip to content

[Bug]: ManyToMany relation with PivotEntity #14363

@dfuri

Description

@dfuri

Package.json file

{
  "name": "medusa-store",
  "version": "0.0.1",
  "description": "A starter for Medusa projects.",
  "author": "Medusa (https://medusajs.com)",
  "license": "MIT",
  "keywords": [
    "sqlite",
    "postgres",
    "typescript",
    "ecommerce",
    "headless",
    "medusa"
  ],
  "scripts": {
    "build:local": "npm --prefix .medusa/server/ --legacy-peer-deps install .medusa/server/ && medusa build",
    "build:dev": "MEDUSA_BACKEND_URL=https://admin.dev.leafcigars.eu NODE_ENV=test medusa build",
    "build:production": "MEDUSA_BACKEND_URL=https://admin.prod.leafcigars.eu NODE_ENV=production medusa build",
    "build:test": "npm run build:dev",
    "seed": "medusa exec ./src/scripts/seed.ts",
    "start": "medusa start",
    "dev": "medusa develop",
    "docker:build:dev": "docker build --build-arg NODE_ENV=test -t medusa-backend . && docker tag medusa-backend medusa-server:latest && docker tag medusa-backend medusa-worker:latest",
    "docker:build:production": "docker build --build-arg NODE_ENV=production -t medusa-backend . && docker tag medusa-backend medusa-server:latest && docker tag medusa-backend medusa-worker:latest",
    "docker:save": "docker save medusa-backend > medusa-backend.tar",
    "docker:save:production": "docker save medusa-backend > medusa-backend-production.tar",
    "docker:logs": "docker logs -f medusa_backend",
    "docker:shell": "docker exec -it medusa_backend sh",
    "predeploy": "npm run predeploy:db && npm run predeploy:meilisearch",
    "predeploy:local": "npm run predeploy:db && npm run predeploy:local:meilisearch",
    "predeploy:db": "medusa db:migrate",
    "predeploy:meilisearch": "medusa exec src/scripts/update-meilisearch-settings.js && medusa exec src/scripts/update-meilisearch-documents.js",
    "predeploy:local:meilisearch": "medusa exec src/scripts/update-meilisearch-settings.ts && medusa exec src/scripts/update-meilisearch-documents.ts",
    "test:integration:http": "TEST_TYPE=integration:http NODE_OPTIONS=--experimental-vm-modules jest --silent=false --runInBand --forceExit",
    "test:integration:modules": "TEST_TYPE=integration:modules NODE_OPTIONS=--experimental-vm-modules jest --silent=false --runInBand --forceExit",
    "test:unit": "TEST_TYPE=unit NODE_OPTIONS=--experimental-vm-modules jest --silent --runInBand --forceExit"
  },
  "dependencies": {
    "@medusajs/admin-sdk": "2.12.2",
    "@medusajs/cli": "2.12.2",
    "@medusajs/framework": "2.12.2",
    "@medusajs/medusa": "2.12.2",
    "@paypal/paypal-server-sdk": "^1.0.0",
    "@perseidesjs/notification-nodemailer": "^1.1.0",
    "date-fns": "^4.1.0",
    "medusa-variant-images": "^1.1.4",
    "nodemailer": "^7.0.6"
  },
  "devDependencies": {
    "@medusajs/test-utils": "2.12.2",
    "@swc/core": "1.5.7",
    "@swc/jest": "^0.2.36",
    "@types/jest": "^29.5.13",
    "@types/node": "^20.0.0",
    "@types/react": "^18.3.2",
    "@types/react-dom": "^18.2.25",
    "jest": "^29.7.0",
    "prop-types": "^15.8.1",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "ts-node": "^10.9.2",
    "typescript": "^5.6.2",
    "vite": "^5.2.11",
    "yalc": "^1.0.0-pre.53"
  },
  "engines": {
    "node": ">=20"
  },
  "packageManager": "[email protected]+sha512.2d92c86b7928dc8284f53494fb4201f983da65f0fb4f0d40baafa5cf628fa31dae3e5968f12466f17df7e97310e30f343a648baea1b9b350685dafafffdf5808"
}

Node.js version

v22.14.0

Database and its version

Postgres 15-alpine (in Docker)

Operating system name and version

Ubuntu 24

Browser name

Chrome

What happended?

Hi all,
at the moment I try to create a ManyToMany relationship between two models (say: Pairing and Flavor, see code below) in a module, but using a PivotEntity (say: PairingFlavorModifier). Principally, I try to perform it as specified in the documentation. Data is written into the pivotEntity table of the database, using the generated service methods of the PairingFlavorModifier model, but I cannot retrieve the data anymore when requesting the related models Pairing or Flavor, which use the pivot. There is also no hint in the documentation, how to retrieve back the data (for the case when using a pivot entity). It contains an empty flavor_modifiers array (not undefined, but empty), when specifying the flavor_modifiers in the relations array of the retrieve service call.

Yes, I can fill the flavor_modifiers field of model Pairing manually by performing an update call on Model Pairing specifying the id of Model Flavor (that is weird, since I expect to specify the id of model PairingFlavorModifier there, which produces an error). However, then the array is filled, but the extra columns of the model PairingFlavorModifier is not contained there, but just the bare data of model Flavor.

Does anyone have experience with these entities or can tell me, where to find documentation or where to ask for help?
Thanks for support!

export const Pairing = model.define("pairing", {
    id: model.id().primaryKey(),
    flavor_modifiers: model.manyToMany(() => Flavor, {
        pivotEntity: () => PairingFlavorModifier,
    }),
})

export const Flavor = model.define("flavor", {
    id: model.id().primaryKey(),
    flavor_modifiers: model.manyToMany(() => Pairing),
})

export const PairingFlavorModifier = model.define("pairing_flavor_modifier", {
    id: model.id().primaryKey(),
    pairing: model.belongsTo(() => Pairing, {
        mappedBy: "flavor_modifiers",
    }),
    flavor: model.belongsTo(() => Flavor, {
        mappedBy: "flavor_modifiers",
    }),
    changesFlavorBy: model.number()
})

Following always works, filling the pivot table

pairingModuleService.createPairingFlavorModifiers({
                    pairing_id: input.id,
                    flavor_id: modifier.flavor_id,
                    changesFlavorBy: modifier.changesFlavorBy,
                })

Following works when using the flavor_id in the flavor_modifiers array. However, I expect to use the id of the pivot entity here, not the flavor_id. Anyway, I do not expect to make this call at all, since I have a relation created above!

pairingModuleService.updatePairings({...data, flavor_modifiers: [flavor_id_0]})

Following, never returns my extra columns of the pivot or no data at all, if I do not execute the updatePairing call above

pairingModuleService.listPairings({
        relations: ["flavor_modifiers"],
    })

Expected behavior

  • The extra field changesFlavorBy can be retrieved by requesting pairing model with a relation set in service method
  • I do not have to call an update on pairing with array of flavor_id set

Actual behavior

  • I do not retrieve the extra columns
  • I have to retrieve the pivotEntity with an extra service call and merge it with the pairing manually for every spot

Link to reproduction repo

Sorry, I do not have one

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