| name | modify-schema |
| description | This skill should be used when the user asks to "alter a table", "rename a column", "change column type", "drop a column", "drop a table", "remove a constraint", "delete a junction table", "add default value", "set NOT NULL", or needs to modify an existing PostgreSQL schema that is connected to NocoDB or NocoBase.
|
Modify Schema — ALTER TABLE & DROP Operations
How to safely modify PostgreSQL tables that are connected as external data sources to NocoDB and NocoBase.
Add Columns
ALTER TABLE "public"."products" ADD "title" text;
ALTER TABLE "public"."products" ADD "quantity" bigint;
ALTER TABLE "public"."products" ADD "price" decimal(10, 2);
ALTER TABLE "public"."products" ADD "discount" double precision;
ALTER TABLE "public"."products" ADD "rating" smallint DEFAULT 0;
ALTER TABLE "public"."products" ADD "is_active" bool DEFAULT false;
ALTER TABLE "public"."products" ADD "published_at" timestamp;
ALTER TABLE "public"."products" ADD "event_at" timestamptz;
ALTER TABLE "public"."products" ADD "birth_date" date;
ALTER TABLE "public"."products" ADD "start_time" time;
ALTER TABLE "public"."products" ADD "metadata" json;
ALTER TABLE "public"."products" ADD "category_id" int4;
Rename Column
ALTER TABLE "public"."products" RENAME COLUMN "old_name" TO "new_name";
Change Column Type
ALTER TABLE "public"."products" ALTER COLUMN "price" DROP DEFAULT;
ALTER TABLE "public"."products" ALTER COLUMN "price" TYPE decimal(12, 4)
USING "price"::decimal(12, 4);
Set / Remove DEFAULT
ALTER TABLE "public"."products" ALTER COLUMN "status" SET DEFAULT 'draft';
ALTER TABLE "public"."products" ALTER COLUMN "status" DROP DEFAULT;
Set / Remove NOT NULL
ALTER TABLE "public"."products" ALTER COLUMN "title" SET NOT NULL;
ALTER TABLE "public"."products" ALTER COLUMN "title" DROP NOT NULL;
Drop Column
ALTER TABLE "public"."products" DROP COLUMN "obsolete_field";
Drop Table
DROP TABLE IF EXISTS "public"."table_name";
Drop FK Constraint
When removing a relation, drop the constraint first, then the FK column:
ALTER TABLE "public"."child_table" DROP CONSTRAINT "fk_name";
ALTER TABLE "public"."child_table" DROP COLUMN "parent_id";
Drop Junction Table (Many-to-Many)
For M2M relations, drop FK constraints before the table:
ALTER TABLE "public"."nc_m2m_a_b" DROP CONSTRAINT "fk_name_1";
ALTER TABLE "public"."nc_m2m_a_b" DROP CONSTRAINT "fk_name_2";
DROP TABLE IF EXISTS "public"."nc_m2m_a_b";
Drop Index
DROP INDEX IF EXISTS "public"."idx_table_column";
Safe Modification Order
When making complex schema changes:
- Drop dependent FK constraints first
- Modify or drop columns
- Add new columns
- Add new FK constraints
- Add indexes on new FK columns