Why it happens: DROP COLUMN fails due to dependencies
SQLite shows the problem in a compact way. If a column appears in an explicit index, the schema can no longer be rebuilt correctly once the column is dropped:
CREATE TABLE demo_customers (
id INTEGER PRIMARY KEY,
legacy_code TEXT,
name TEXT NOT NULL
);
CREATE INDEX idx_demo_code
ON demo_customers(legacy_code);
ALTER TABLE demo_customers
DROP COLUMN legacy_code;In a recent version of SQLite, the last statement fails because the index idx_demo_code still references legacy_code. The engine tries to rebuild the schema and finds that an invalid reference would be left behind.
How to fix it in SQL
In the SQLite example, the safe sequence is explicit:
DROP INDEX idx_demo_code;
ALTER TABLE demo_customers
DROP COLUMN legacy_code;If the dependency is functional, such as a view that still exposes the column, it is not enough to “make the DDL go through”. First decide what that view should look like, then change it or remove it. The migration should preserve what the system is meant to do, not just avoid an error message.
How to check it
Review the schema after the dry run and run the queries that depended on the table.
How to avoid it next time
CASCADE can remove extra objects; list everything it would affect before using it.
Related cases worth checking
The real cause: the schema is a network of dependencies
A column can be part of much more than a table. Before dropping it, check at least:
- indexes that use it as a key or included column;
- PRIMARY KEY, UNIQUE, CHECK or FOREIGN KEY;
- views that select or filter on it;
- triggers and generated column expressions;
- procedures or application code that still expect to find it.
Diagnosis: find out what depends on the column
- Confirm the exact column: table, schema and real name.
- List constraints and indexes: do not assume a constraint's name matches the column's.
- Look for external references: other tables, views, triggers and generated columns.
- Classify each dependency: should it disappear, move to another column or stay?
- Plan the order of the migration: remove the dependencies first and the column last.
In production, use the engine's catalogues and your migration history to build this inventory. Do not rely only on a text search of the code: an old migration may have created a constraint or an index.
What changes between engines
PostgreSQL
DROP COLUMN automatically removes the table's own indexes and constraints that involve the column. If there are dependent external objects, such as a view or a foreign key from another table, the operation is rejected by default. CASCADE allows PostgreSQL to remove the dependent objects too.
SQL Server
A column cannot be dropped while it is used by certain indexes, CHECK, FOREIGN KEY, UNIQUE, PRIMARY KEYconstraints, associated defaults or other documented dependencies. Remove those dependencies first and then run DROP COLUMN.
SQLite
SQLite rejects DROP COLUMN if the column is a PRIMARY KEY or UNIQUE, or still appears elsewhere in the schema: indexes, partial indexes, CHECKconstraints, foreign keys, generated columns, triggers or views. The operation only completes if the whole schema can still be parsed without that column.
MySQL
MySQL removes a column from the indexes it belongs to, and can drop an index that is left with no columns. Not every dependency is handled the same way, though: foreign keys and generated columns may require you to remove or redefine the relationship first. Check SHOW CREATE TABLE and the catalogue before assuming what will disappear automatically.
A safe migration in four stages
- Stop writing to the column: first deploy code that no longer depends on it.
- Stop reading it: check views, reports, queries and external processes.
- Remove dependencies: indexes, constraints, triggers or generated columns that no longer make sense.
- Drop the column: run
DROP COLUMNas the final step and check the resulting schema.
Separating these stages reduces the risk of deploying a new application and a destructive migration at the same time. For large tables, also check the cost and locks that the operation may require in your database engine.