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]: Cannot Define link modules to the same Module twice #10827

Open
kowalski21 opened this issue Jan 5, 2025 · 1 comment
Open

[Bug]: Cannot Define link modules to the same Module twice #10827

kowalski21 opened this issue Jan 5, 2025 · 1 comment

Comments

@kowalski21
Copy link

Package.json file

{
  "name": "medusa-starter-default",
  "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": "medusa build",
    "hello": "medusa exec ./src/scripts/hello.ts",
    "workflow": "medusa exec ./src/scripts/workflow.ts",
    "start": "medusa start",
    "dev": "medusa develop",
    "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 --runInBand --forceExit",
    "test:unit": "TEST_TYPE=unit NODE_OPTIONS=--experimental-vm-modules jest --silent --runInBand --forceExit"
  },
  "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.7",
    "@mikro-orm/knex": "5.9.7",
    "@mikro-orm/migrations": "5.9.7",
    "@mikro-orm/postgresql": "5.9.7",
    "awilix": "^8.0.1",
    "knex": "^3.1.0",
    "pg": "^8.13.1"
  },
  "devDependencies": {
    "@medusajs/test-utils": "2.1.3",
    "@mikro-orm/cli": "5.9.7",
    "@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"
  },
  "engines": {
    "node": ">=20"
  }
}

Node.js version

v20.15.0

Database and its version

PostgreSQL 16.2

Operating system name and version

MacOS Sequioa Version 15.2

Browser name

Google Chrome

What happended?

  • I am trying to create StockTransfer Module. The goal is to allow to transfer stock quantity of an inventoryItem from one stock location to the another.I want to track the quantity transfered, the stock location that the stock was moved from, the stock location that the quantity was moved to and the product variant involved. To do this I
  • Define StockTransfer model with columns of id and qty
  • Then I proceed to create link from StockTransfer to StockLocation
    This is the link definition. This is to track the stock location where we are decrementing the quantity.
import { defineLink } from "@medusajs/framework/utils";
import StockLocationModule from "@medusajs/medusa/stock-location";

import VendorModule from "src/modules/vendor";

export default defineLink(VendorModule.linkable.stockTransfer, StockLocationModule.linkable.stockLocation, {
  database: {
    extraColumns: {
      from_qty: {
        type: "integer",
      },
    },
  },
});

Then another link definition from StockTransfer model to StockLocation model. The purpose of the link definition is to track the new quantity to be added to the new stock location. Below is the link definition

import { defineLink } from "@medusajs/framework/utils";
import StockLocationModule from "@medusajs/medusa/stock-location";

import VendorModule from "src/modules/vendor";

export default defineLink(VendorModule.linkable.stockTransfer, StockLocationModule.linkable.stockLocation, {
  database: {
    extraColumns: {
      to_qty: {
        type: "integer",
      },
    },
  },
});

Then finally the linking between the StockTransfer model and ProductVariant model.

// link from vendor stock to product variant
import VendorModule from "src/modules/vendor";
import { defineLink } from "@medusajs/framework/utils";
import ProductModule from "@medusajs/medusa/product";

export default defineLink(VendorModule.linkable.vendorStock, ProductModule.linkable.productVariant);

Now when I run npx medusa db:generate to create the links, I run into the error below
Error initializing link modules. Error: Link module VendorStockTransferStockLocationStockLocationLink already defined.

This means that its not possible to create 2 links from one model to another core model.

I can implement this using the following using the model below

export const StockTransfer = model.define("stock_transfer", {
  id: model.id({ prefix: "ste" }).primaryKey(),
  from_qty: model.number(),
  to_qty: model.number(),
  from_stocklocation: model.text(),
  to_stocklocation: model.text(),
});

Then create joins using the "__pg_connection__" module but it goes against
the philosophy of each module being independent on its own.

Expected behavior

There should not be any error and linking modules should be created

Actual behavior

Error initializing link modules. Error: Link module VendorStockTransferStockLocationStockLocationLink already defined.

Link to reproduction repo

https://github.com/kowalski21/medusa-pharmacy

@carlos-r-l-rodrigues
Copy link
Contributor

Hi @kowalski21,
currently we do not support the same link definition between two modules using the exact same keys.
I believe in your case you can have a single link with extra fields: to, from, and qty to track everything.
The main stock_transfer_id + stock_location_id is the starting point, and to/from is the destination.

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