{"record":{"id":"ef67f269d596ff83","repo":"medusajs/medusa","slug":"countries-with-codes-countrieswithregion-map","errorCode":null,"errorMessage":"Countries with codes: \"${countriesWithRegion.map((c) => c.iso_2).join(\", \")}\" are already assigned to a region","messagePattern":"Countries with codes: \"(.+?)\" are already assigned to a region","errorType":"validation","errorClass":"MedusaError","httpStatus":400,"severity":"error","filePath":"packages/modules/region/src/services/region-module.ts","lineNumber":369,"sourceCode":"\n    // Countries missing in the database\n    if (countriesInDb.length !== uniqueCountries.length) {\n      const missingCountries = arrayDifference(\n        uniqueCountries,\n        countryCodesInDb\n      )\n\n      throw new MedusaError(\n        MedusaError.Types.INVALID_DATA,\n        `Countries with codes: \"${missingCountries.join(\", \")}\" do not exist`\n      )\n    }\n\n    // Countries that already have a region already assigned to them\n    // @ts-ignore\n    const countriesWithRegion = countriesInDb.filter((c) => !!c.region_id)\n    if (countriesWithRegion.length) {\n      throw new MedusaError(\n        MedusaError.Types.INVALID_DATA,\n        `Countries with codes: \"${countriesWithRegion\n          .map((c) => c.iso_2)\n          .join(\", \")}\" are already assigned to a region`\n      )\n    }\n\n    return countriesInDb\n  }\n}\n","sourceCodeStart":351,"sourceCodeEnd":380,"githubUrl":"https://github.com/medusajs/medusa/blob/5e06e544a296b9033f20f71f11c559f81a0e5739/packages/modules/region/src/services/region-module.ts#L351-L380","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","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"],"exampleFix":"// before\n// 'de' already belongs to regionEU\nawait regionModule.updateRegions({ selector: { id: regionNAId }, data: { countries: [\"us\", \"de\"] } })\n// after\nawait regionModule.updateRegions({ selector: { id: regionEUId }, data: { countries: [] } })\nawait regionModule.updateRegions({ selector: { id: regionNAId }, data: { countries: [\"us\", \"de\"] } })","handlingStrategy":"validation","validationCode":"const assigned = await regionModule.listCountries({ filter: { iso_2: requestedCountries } })\nconst taken = assigned.filter((c) => c.region_id && c.region_id !== currentRegionId).map((c) => c.iso_2)\nif (taken.length) throw new Error(`Countries already assigned elsewhere: ${taken.join(\", \")}`)","typeGuard":"function isCountryFree(c: { region_id?: string | null }, currentRegionId?: string): boolean {\n  return !c.region_id || c.region_id === currentRegionId\n}","tryCatchPattern":"try {\n  await regionModule.updateRegions(args)\n} catch (e) {\n  if (/already assigned to a region/.test(e.message)) {\n    // free the country from its current region first, then retry\n  } else throw e\n}","preventionTips":["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)"],"tags":["region","countries","conflict","validation"],"backgroundTag":"unique-constraint-conflict","analyzedSha":"5e06e544a296b9033f20f71f11c559f81a0e5739","analyzedAt":"2026-08-27T07:24:39.599Z","schemaVersion":2},"datasetVersion":"2026-08-27T08:17:20.692Z"}