Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Attributes of 1:1 relationship table are not being updated #10948

Open
ismael-uplabs opened this issue Jan 13, 2025 · 1 comment
Open

Comments

@ismael-uplabs
Copy link

ismael-uplabs commented Jan 13, 2025

Package.json file

{
  "name": "medusa2",
  "version": "2.0.0",
  "description": "A starter for Medusa projects.",
  "author": "Medusa (https://medusajs.com)",
  "license": "MIT",
  "keywords": [
    "sqlite",
    "postgres",
    "typescript",
    "ecommerce",
    "headless",
    "medusa"
  ],
  "installConfig": {
    "hoistingLimits": "workspaces"
  },
  "scripts": {
    "medusa2:build": "medusa build",
    "medusa2:start": "medusa start",
    "medusa2:dev": "medusa develop",
    "start:prod": "medusa start",
    "medusa2:migrate": "medusa db:migrate",
    "medusa2:test:integration:http": "TEST_TYPE=integration:http NODE_OPTIONS=--experimental-vm-modules jest --silent=true --forceExit",
    "medusa2:test:integration:modules": "TEST_TYPE=integration:modules NODE_OPTIONS=--experimental-vm-modules jest --silent --forceExit",
    "medusa2:test:integration:workflows": "TEST_TYPE=integration:workflows NODE_OPTIONS=--experimental-vm-modules jest --silent --forceExit",
    "medusa2:test:unit": "TEST_TYPE=unit NODE_OPTIONS=--experimental-vm-modules jest --silent --runInBand --forceExit",
    "medusa2:typecheck": "tsc --noEmit"
  },
  "dependencies": {
    "@medusajs/admin-sdk": "2.1.3",
    "@medusajs/cli": "2.1.3",
    "@medusajs/framework": "2.1.3",
    "@medusajs/medusa": "2.1.3",
    "@mikro-orm/core": "5.9.8",
    "@mikro-orm/knex": "5.9.8",
    "@mikro-orm/migrations": "5.9.8",
    "@mikro-orm/postgresql": "5.9.8",
    "@segment/analytics-node": "2.2.0",
    "awilix": "8.0.1",
    "pg": "8.13.1",
    "stripe": "17.4.0"
  },
  "devDependencies": {
    "@descend/sdk": "0.0.1",
    "@medusajs/test-utils": "2.1.3",
    "@mikro-orm/cli": "5.9.8",
    "@swc/core": "1.10.4",
    "@swc/jest": "0.2.37",
    "@types/jest": "29.5.14",
    "@types/node": "22.10.5",
    "@types/react": "18.3.18",
    "@types/react-dom": "18.3.5",
    "csv-parse": "5.6.0",
    "jest": "29.7.0",
    "prop-types": "15.8.1",
    "react": "18.3.1",
    "react-dom": "18.3.1",
    "ssh2-sftp-client": "11.0.0",
    "ts-node": "10.9.2",
    "typescript": "5.7.2",
    "vite": "6.0.7"
  },
  "engines": {
    "node": ">=20"
  }
}

Node.js version

v20.15.0

Database and its version

Postgres v15.8

Operating system name and version

MacOS Sequoia 15.2 (24C101)

Browser name

No response

What happended?

I have a Partner table with the following definition:

export const Partner = model.define('partner', {
  id: model.id({ prefix: 'partner' }).primaryKey(),
  name: model.text(),
  status: model.enum([...partnerStatuses]).default('pending'),
  address: model.hasOne(() => PartnerAddress).nullable(),
});

PartnerAddress

export const PartnerAddress = model.define('partner_address', {
  id: model.id({ prefix: 'part_addr' }).primaryKey(),
  address_1: model.text(),
  address_2: model.text().nullable(),
  city: model.text().nullable(),
  province: model.text().nullable(),
  postal_code: model.text().nullable(),
  country_code: model.text().nullable(),
  metadata: model.json().nullable(),
  partner: model.belongsTo(() => Partner, {
    mappedBy: 'address',
  }),
});

I created an Step called update-partner-step.ts with the following code:

    const partnerService = container.resolve<PartnerService>(PARTNER_MODULE);

    const existingPartner = await partnerService.retrievePartner(data.id, {
      relations: ['address'],
    });

    if (!existingPartner) throw new MedusaError(MedusaError.Types.NOT_FOUND, 'Partner not found');

    if (existingPartner.address?.id && data.address) {
      data.address.id = existingPartner.address.id;
    }
    
    const updatedPartner = await partnerService.updatePartners(data);

With the following input:

{
    "name": "Test Partner1",
    "address": {
        "address_1": "Some street 1",
        "address_2": "17",
        "city": "Ontario"
    }
}

The object returned by await partnerService.updatePartners(data) has the updated data, however seems the DB is not being updated at all, unless not the attributes under address object.

Expected behavior

Update the records in DB

Actual behavior

DB records not being updated

Link to reproduction repo

https://github.com/SaySo-co/medusa2

@thetutlage
Copy link
Contributor

Hello @ismael-uplabs

If I am correct, you cannot update nested data using services and instead you must call the updateAddresses method separately to update the address.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants