Skip to content

[Bug]: v2.7.0 cannot create multiple links between 'blog' and 'user' #12236

Closed as not planned
@hokkimlong

Description

@hokkimlong

Package.json file

{
  "name": "medusa-api",
  "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",
    "seed": "medusa exec ./src/scripts/seed.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.7.0",
    "@medusajs/cli": "2.7.0",
    "@medusajs/file-s3": "2.7.0",
    "@medusajs/framework": "2.7.0",
    "@medusajs/medusa": "2.7.0",
    "@mikro-orm/core": "6.4.3",
    "@mikro-orm/knex": "6.4.3",
    "@mikro-orm/migrations": "6.4.3",
    "@mikro-orm/postgresql": "6.4.3",
    "awilix": "^8.0.1",
    "big.js": "^6.2.2",
    "pg": "^8.13.0",
    "quill-resize-image": "^1.0.5",
    "react-quill-new": "^3.4.1"
  },
  "devDependencies": {
    "@medusajs/test-utils": "2.7.0",
    "@mikro-orm/cli": "6.4.3",
    "@swc/core": "1.5.7",
    "@swc/jest": "^0.2.36",
    "@types/big.js": "^6.2.2",
    "@types/jest": "^29.5.13",
    "@types/multer": "^1.4.12",
    "@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.16.0

Database and its version

Postgres 17.4

Operating system name and version

Mac 15.1

Browser name

Chrome

What happended?

After update to v2.7.0 got issue with unique constraint but still worked on v2.6.1

Feature

  • Create blog link user as author

Issue

  • Each user can no longer create more than one blog got error after created
error:   Cannot create multiple links between 'blog' and 'user'
MedusaError: Cannot create multiple links between 'blog' and 'user'
    at Link.create (/Users/user/Work/medusa-api/node_modules/@medusajs/modules-sdk/src/link.ts:499:17)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async POST (/Users/user/Work/medusa-api/src/api/admin/blogs/route.ts:50:3)
    at async POST (/Users/user/Work/medusa-apinode_modules/@medusajs/framework/src/http/utils/wrap-handler.ts:27:14) {
  __isMedusaError: true,
  type: 'invalid_data',
  code: undefined,
  date: 2025-04-18T08:24:45.023Z
}

Link

import UserModule from "@medusajs/medusa/user";
import { defineLink } from "@medusajs/framework/utils";
import BlogModule from "../modules/blog";

export default defineLink(BlogModule.linkable.blog, UserModule.linkable.user);

Create Blog

export const POST = async (
  req: AuthenticatedMedusaRequest<PostAdminCreateBlogType>,
  res: MedusaResponse
) => {
  const id = req.auth_context?.actor_id;
  const handle = req.validatedBody.handle || toHandle(req.validatedBody.title);

  const query: BlogModuleService = req.scope.resolve(BLOG_MODULE);
  const result = await query.createBlogs({
    ...req.validatedBody,
    handle,
    published_at: new Date(),
  });

  if (!result) {
    return res.sendStatus(400).json({
      message: "Failed to create blog",
    });
  }

  const link = req.scope.resolve(ContainerRegistrationKeys.LINK);

  await link.create({
    [BLOG_MODULE]: {
      blog_id: result.id,
    },
    [Modules.USER]: {
      user_id: user.id,
    },
  });

  return res.json({ data: result });
};

Expected behavior

Each user can create more than one blog and shouldn't have unique constraint on user.

Actual behavior

Each user can no longer create more than one blog and have unique constraint on user.

Link to reproduction repo

/

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