SQL error · INSERT, UPDATE, DELETE and schema

DROP COLUMN fails due to dependencies

Problem: The column is still used by indexes, views or constraints.

Solution: Identify each dependency and change or remove only the ones that need it before dropping the column.

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:

A dependency that blocks DROP COLUMN
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.

“DROP COLUMN fails due to dependencies” error messages by engine

The exact wording depends on the database. Compare these common messages with the one in your console.

EngineTypical message
PostgreSQLERROR: cannot drop column price of table products because other objects depend on it
MySQLERROR 1829 (HY000): Cannot drop column 'category_id': needed in a foreign key constraint 'fk_products_category'
SQL ServerMsg 5074, Level 16: The object 'DF_products_price' is dependent on column 'price'. Msg 4922: ALTER TABLE DROP COLUMN price failed because one or more objects access this column.
OracleORA-12992: cannot drop parent key column

In SQLite, DROP COLUMN fails if the column is part of an index, constraint, or view.

How to fix it in SQL

In the SQLite example, the safe sequence is explicit:

Correct order
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.
Key idea: first stop using the column; then remove its schema dependencies; only then drop the column.

Diagnosis: find out what depends on the column

  1. Confirm the exact column: table, schema and real name.
  2. List constraints and indexes: do not assume a constraint's name matches the column's.
  3. Look for external references: other tables, views, triggers and generated columns.
  4. Classify each dependency: should it disappear, move to another column or stay?
  5. 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.

Do not use CASCADE as a shortcut. Before running it, identify exactly which objects will disappear. A migration that “works” but removes a view you need is still a broken migration.

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

  1. Stop writing to the column: first deploy code that no longer depends on it.
  2. Stop reading it: check views, reports, queries and external processes.
  3. Remove dependencies: indexes, constraints, triggers or generated columns that no longer make sense.
  4. Drop the column: run DROP COLUMN as 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.