← Back to SQL, page 13

sql-0244 · Medium mid

A SQLite view refers to a column that was later renamed in the base table. What happens when you query the view?

Since SQLite 3.25, ALTER TABLE RENAME COLUMN also rewrites references in views and triggers, so the view keeps working, though its unaliased output column takes the new name. Dropping the column, or recreating the table under a new definition without the column, leaves the view broken, and the error appears only when the view is queried. PostgreSQL tracks view dependencies and refuses to drop a column a view uses unless CASCADE is given.

Full answer
sql
CREATE TABLE departments (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL
);
CREATE TABLE employees (
  id         INTEGER PRIMARY KEY,
  name       TEXT NOT NULL,
  dept_id    INTEGER REFERENCES departments(id),
  manager_id INTEGER REFERENCES employees(id),
  salary     INTEGER,
  hire_date  TEXT
);
INSERT INTO departments VALUES (1,'Engineering'),(2,'Sales'),(3,'Support'),(4,'Legal');
INSERT INTO employees VALUES
  (1,'Asha', 1,NULL,120000,'2019-03-01'),
  (2,'Ravi', 1,1,    95000,'2020-07-15'),
  (3,'Mei',  1,1,    95000,'2021-01-10'),
  (4,'Tom',  2,1,    70000,'2022-05-20'),
  (5,'Sara', 2,4,    62000,'2023-02-01'),
  (6,'Diego',3,1,    55000,'2021-11-30'),
  (7,'Lena', NULL,4, NULL,  '2024-06-03'),
  (8,'Omar', 3,6,    48000,'2024-09-09');

Renaming is handled:

sql
CREATE VIEW pay AS SELECT name, salary FROM employees;
ALTER TABLE employees RENAME COLUMN salary TO base_salary;
SELECT * FROM pay WHERE name = 'Asha';
namebase_salary
Asha120000

The view still works, but its output column is now called base_salary, because SQLite rewrote the view text. Queries that select salary from the view would fail. Giving view columns explicit aliases (base_salary AS salary) keeps the view's interface stable across renames.

Dropping a column the view uses is refused in SQLite 3.35+, because the view would break:

sql
CREATE VIEW pay2 AS SELECT name, hire_date FROM employees;
ALTER TABLE employees DROP COLUMN hire_date;
-- error on purpose
text
Error: error in view pay2 after drop column: no such column: hire_date

PostgreSQL is stricter in the same situation and names the dependent view:

sql
CREATE VIEW pay AS SELECT name, salary FROM employees;
ALTER TABLE employees DROP COLUMN salary;
-- error on purpose
text
Error: cannot drop column salary of table employees because other objects depend on it

When views break after a migration, list them with SELECT name, sql FROM sqlite_schema WHERE type = 'view', or in PostgreSQL use the dependency views, and recreate them as part of the migration.

sql
CREATE TABLE departments (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL
);
CREATE TABLE employees (
  id         INTEGER PRIMARY KEY,
  name       TEXT NOT NULL,
  dept_id    INTEGER REFERENCES departments(id),
  manager_id INTEGER REFERENCES employees(id),
  salary     INTEGER,
  hire_date  TEXT
);
INSERT INTO departments VALUES (1,'Engineering'),(2,'Sales'),(3,'Support'),(4,'Legal');
INSERT INTO employees VALUES
  (1,'Asha', 1,NULL,120000,'2019-03-01'),
  (2,'Ravi', 1,1,    95000,'2020-07-15'),
  (3,'Mei',  1,1,    95000,'2021-01-10'),
  (4,'Tom',  2,1,    70000,'2022-05-20'),
  (5,'Sara', 2,4,    62000,'2023-02-01'),
  (6,'Diego',3,1,    55000,'2021-11-30'),
  (7,'Lena', NULL,4, NULL,  '2024-06-03'),
  (8,'Omar', 3,6,    48000,'2024-09-09');

CREATE VIEW pay AS SELECT name, salary FROM employees;
ALTER TABLE employees RENAME COLUMN salary TO base_salary;
SELECT * FROM pay WHERE name = 'Asha';

-- error on purpose
CREATE VIEW pay2 AS SELECT name, hire_date FROM employees;
ALTER TABLE employees DROP COLUMN hire_date;

Checked on SQLite 3.46.1, run twice with identical output. PostgreSQL results in the answer were checked on PostgreSQL 17.12.

Key points

  • SQLite 3.25+ RENAME COLUMN updates views and triggers
  • Dropping a used column is refused
  • PostgreSQL tracks dependencies and needs CASCADE
  • Recreate dependent views in migrations

Common mistakes

  • Discovering broken views only when a report fails
  • Dropping columns with CASCADE without checking what it removes

Follow-ups

  • How would you find all objects that depend on a column?
  • How would you change a column used by many views?

Tips

  • Check dependencies before migrations