ALTER TABLE "custom_fields" ADD COLUMN "key" text; --> statement-breakpoint UPDATE "custom_fields" SET "key" = trim(both '_' from regexp_replace(lower("name"), '[^a-z0-9]+', '_', 'g')); --> statement-breakpoint UPDATE "custom_fields" SET "key" = 'field_' || substring("id"::text, 1, 8) WHERE "key" IS NULL OR "key" = ''; --> statement-breakpoint ALTER TABLE "custom_fields" ALTER COLUMN "key" SET NOT NULL; --> statement-breakpoint ALTER TABLE "custom_fields" ADD CONSTRAINT "custom_fields_key_unique" UNIQUE("key");