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

  1. First remove the country from its current region (updateRegions on the old region without it), then add it to the new region
  2. If re-seeding, delete/reset existing regions first or filter out already-assigned countries
  3. 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

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


AI-assisted analysis of medusajs/medusa@5e06e544a2 (2026-08-27). Data as JSON: /api/errors/ef67f269d596ff83. Report an issue: GitHub.