medusajs/medusa · error · MedusaError
Cannot associate duplicate inventory items to variant(s) ${e
Error message
Cannot associate duplicate inventory items to variant(s) ${erroredVariantIds.join("\n")} What it means
Thrown by validateVariantsDuplicateInventoryItemIds when creating product variants whose inventory item links would assign the same inventory item to multiple variants (or the same item twice within one variant). Medusa prevents this because inventory levels are tracked per inventory item, and duplicating associations would corrupt stock accounting. It is an INVALID_DATA error raised during workflow validation before any data is written.
Source
Thrown at packages/core/core-flows/src/product/workflows/create-product-variants.ts:103
}[]
) => {
const erroredVariantIds: string[] = []
for (const variantData of variantsData) {
const inventoryItemIds = variantData.inventory_items.map(
(item) => item.inventory_item_id
)
const duplicatedInventoryItemIds = inventoryItemIds.filter(
(id, index) => inventoryItemIds.indexOf(id) !== index
)
if (duplicatedInventoryItemIds.length) {
erroredVariantIds.push(variantData.variantId)
}
}
if (erroredVariantIds.length) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
`Cannot associate duplicate inventory items to variant(s) ${erroredVariantIds.join(
"\n"
)}`
)
}
}
const buildLinksToCreate = (data: {
createdVariants: ProductTypes.ProductVariantDTO[]
inventoryIndexMap: Record<number, InventoryTypes.InventoryItemDTO>
input: CreateProductVariantsWorkflowInput
}) => {
let index = 0
const linksToCreate: LinkDefinition[] = []
validateVariantsDuplicateInventoryItemIds(
(data.createdVariants ?? []).map((variant, index) => {View on GitHub (pinned to 5e06e544a2)
Solutions
- Inspect the workflow input and deduplicate: ensure each inventory_item_id appears in only one variant's inventory_items and only once per variant
- If variants should share stock, keep a single variant or create separate inventory items per variant and adjust stock afterwards
- Re-run the workflow after fixing the input payloads
Example fix
// before
const variants = [
{ title: 'S', inventory_items: [{ inventory_item_id: 'iitem_1', required_quantity: 1 }] },
{ title: 'M', inventory_items: [{ inventory_item_id: 'iitem_1', required_quantity: 1 }] }, // duplicate!
]
// after
const variants = [
{ title: 'S', inventory_items: [{ inventory_item_id: 'iitem_1', required_quantity: 1 }] },
{ title: 'M', inventory_items: [{ inventory_item_id: 'iitem_2', required_quantity: 1 }] },
] Defensive patterns
Strategy: validation
Validate before calling
const seen = new Set<string>()
for (const v of variants) {
for (const ii of v.inventory_items ?? []) {
if (seen.has(ii.inventory_item_id)) throw new Error(`duplicate inventory item ${ii.inventory_item_id}`)
seen.add(ii.inventory_item_id)
}
} Type guard
const hasUniqueInventoryItems = (variants: CreateProductVariantInput[]): boolean => {
const seen = new Set<string>()
return variants.every((v) => (v.inventory_items ?? []).every((ii) => !seen.has(ii.inventory_item_id) && seen.add(ii.inventory_item_id)))
} Try / catch
try { await createProductVariantsWorkflow(scope).run({ input }) } catch (e) { if (e instanceof MedusaError && e.type === MedusaError.Types.INVALID_DATA && /duplicate inventory items/.test(e.message)) { /* dedupe input and retry */ } else throw e } Prevention
- Generate a fresh inventory item per variant instead of reusing ids
- Never copy inventory_items arrays when cloning variant payloads
- Run a dedupe check on inventory_item_id across the whole batch before invoking the workflow
When it happens
Trigger: Calling createProductVariantsWorkflow (or the admin POST /admin/products/:id/variants endpoint) with variant input where inventory_items arrays reference the same inventory_item_id across multiple variants, or the same id repeated within one variant's inventory_items list.
Common situations: Copying variant payload from an existing variant and forgetting to regenerate inventory items; mapping variants to a shared inventory pool incorrectly; migration scripts that assign one inventory item to every variant of a product.
Related errors
- Product options are not provided for: [${missingOptionsProdu
- --paths must be a directory - ${additionalPath}
- --base must be a file - ${baseFile}
- insufficient_inventory
- not_found
AI-assisted analysis of medusajs/medusa@5e06e544a2 (2026-08-27).
Data as JSON: /api/errors/7d753f1bfa0bbe6f.
Report an issue: GitHub.