medusajs/medusa · error · MedusaError
Countries with codes: "${countriesWithRegion.map((c) => c.is
Error message
Countries with codes: "${countriesWithRegion.map((c) => c.iso_2).join(", ")}" are already assigned to a region What it means
Thrown by the region module's validateCountries when one or more of the requested countries are already assigned to another region. Countries have a region_id in the DB, and any conflict is rejected with INVALID_DATA listing the offending iso_2 codes.
Source
Thrown at packages/modules/region/src/services/region-module.ts:369
// Countries missing in the database
if (countriesInDb.length !== uniqueCountries.length) {
const missingCountries = arrayDifference(
uniqueCountries,
countryCodesInDb
)
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
`Countries with codes: "${missingCountries.join(", ")}" do not exist`
)
}
// Countries that already have a region already assigned to them
// @ts-ignore
const countriesWithRegion = countriesInDb.filter((c) => !!c.region_id)
if (countriesWithRegion.length) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
`Countries with codes: "${countriesWithRegion
.map((c) => c.iso_2)
.join(", ")}" are already assigned to a region`
)
}
return countriesInDb
}
}
View on GitHub (pinned to 5e06e544a2)
Solutions
- First remove the country from its current region (updateRegions on the old region without it), then add it to the new region
- If re-seeding, delete/reset existing regions first or filter out already-assigned countries
- For payload-internal conflicts, deduplicate and ensure each country appears in only one region per call
Example fix
// before
// 'de' already belongs to regionEU
await regionModule.updateRegions({ selector: { id: regionNAId }, data: { countries: ["us", "de"] } })
// after
await regionModule.updateRegions({ selector: { id: regionEUId }, data: { countries: [] } })
await regionModule.updateRegions({ selector: { id: regionNAId }, data: { countries: ["us", "de"] } }) Defensive patterns
Strategy: validation
Validate before calling
const assigned = await regionModule.listCountries({ filter: { iso_2: requestedCountries } })
const taken = assigned.filter((c) => c.region_id && c.region_id !== currentRegionId).map((c) => c.iso_2)
if (taken.length) throw new Error(`Countries already assigned elsewhere: ${taken.join(", ")}`) Type guard
function isCountryFree(c: { region_id?: string | null }, currentRegionId?: string): boolean {
return !c.region_id || c.region_id === currentRegionId
} Try / catch
try {
await regionModule.updateRegions(args)
} catch (e) {
if (/already assigned to a region/.test(e.message)) {
// free the country from its current region first, then retry
} else throw e
} Prevention
- Check existing country assignments before moving countries between regions
- Remove from the old region and add to the new region as two coordinated updates
- Make region seed scripts idempotent (skip already-assigned countries)
When it happens
Trigger: Calling createRegions with countries that belong to an existing region, or updateRegions on region A with countries currently assigned to region B (removing from B and adding to A in one call is not supported this way).
Common situations: Trying to move a country between regions by only updating the target region, re-running seeds after regions were already created, or two regions in the payload claiming the same country.
Related errors
- Countries with codes: "${getDuplicates(countries).join(", ")
- Countries with codes: "${missingCountries.join(", ")}" do no
- Country with code ${shippingAddress.country_code} is not wit
- NOT_ALLOWED
- --paths must be a directory - ${additionalPath}
AI-assisted analysis of medusajs/medusa@5e06e544a2 (2026-08-27).
Data as JSON: /api/errors/ef67f269d596ff83.
Report an issue: GitHub.