diff --git a/backend/src/db/migrations/20251018061215_sub-org.ts b/backend/src/db/migrations/20251018061215_sub-org.ts index bd577fe75..d048a23d3 100644 --- a/backend/src/db/migrations/20251018061215_sub-org.ts +++ b/backend/src/db/migrations/20251018061215_sub-org.ts @@ -14,14 +14,10 @@ export async function up(knex: Knex): Promise { t.foreign("rootOrgId").references("id").inTable(TableName.Organization).onDelete("CASCADE"); t.dropUnique(["slug"]); + t.unique(["rootOrgId", "parentOrgId", "slug"]); }); // had to switch to raw for null not distinct - await knex.raw(` -ALTER TABLE "organization" -ADD CONSTRAINT "organization_root_parent_slug_unique" -UNIQUE ("rootOrgId", "parentOrgId", "slug") NULLS NOT DISTINCT; -`); } const hasIdentityOrgCol = await knex.schema.hasColumn(TableName.Identity, "orgId"); diff --git a/backend/src/services/org/org-service.ts b/backend/src/services/org/org-service.ts index 6334322eb..265612d55 100644 --- a/backend/src/services/org/org-service.ts +++ b/backend/src/services/org/org-service.ts @@ -516,6 +516,12 @@ export const orgServiceFactory = ({ } } + if (slug) { + const existingOrg = await orgDAL.findOne({ slug, rootOrgId: null }); + if (existingOrg && existingOrg?.id !== orgId) + throw new BadRequestError({ message: `Organization with slug ${slug} already exist` }); + } + if (googleSsoAuthEnforced) { if (googleSsoAuthEnforced && currentOrg.authEnforced) { throw new BadRequestError({