Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Databases and Redis

DROP TABLE is refused because a view or foreign key points at it

ErrorERROR: cannot drop table users because other objects depend on it

Short answer

DROP TABLE refuses by default when a view or another table's foreign key depends on the table. The DETAIL line names each dependent object. Drop or change that object first, or use DROP TABLE ... CASCADE, which removes dependent views and foreign key constraints but never the other tables.

  • Applies to PostgreSQL 16, PostgreSQL 17, PostgreSQL 18
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Read the DETAIL lines under the error. Each one names an object that depends on the table. Do not run CASCADE before you have read them.

    DROP TABLE users;
  2. 2Match what you see, then jump to its fix

    1. 1DETAIL: constraint orders_user_id_fkey on table orders depends on table usersAnother table has a foreign key to this tableFix
    2. 2DETAIL: view active_users depends on table usersA view reads from the tableFix
    3. 3The goal is to reload the data, not to remove the table for good.You are rebuilding the table and want to keep its dependentsFix

What does the error look like?

  • ERROR: cannot drop table users because other objects depend on it
  • HINT: Use DROP ... CASCADE to drop the dependent objects too.

Cause 1 of 3

Another table has a foreign key to this table

1Check

List the foreign keys that reference the table. A constraint depends on the table it points at.

psql -X -d mydb -c '\d orders'

2Fix

If the other table must stay, drop only that constraint, then drop the table. With CASCADE, PostgreSQL removes only the foreign key constraint, not the other table.

ALTER TABLE orders DROP CONSTRAINT orders_user_id_fkey;
DROP TABLE users;

3Verify

The drop succeeds and the orders table still exists.

psql -X -d mydb -c '\dt'

Confirmed by postgresql.org

Did this fix it?

Cause 2 of 3

A view reads from the table

1Check

RESTRICT, the default, refuses to drop a table when a view uses it. Find the view named in DETAIL and decide whether it is still needed.

psql -X -d mydb -c '\d+ active_users'

2Fix

Drop the view first if it is not needed, or use CASCADE if the view should disappear too. CASCADE removes the dependent view entirely.

DROP VIEW active_users;
DROP TABLE users;
-- or, in one step, if the view is expendable:
DROP TABLE users CASCADE;

3Verify

Both objects are gone and nothing else was removed.

psql -X -d mydb -c '\d'

Confirmed by postgresql.org

Did this fix it?

Cause 3 of 3

You are rebuilding the table and want to keep its dependents

1Check

CASCADE would delete the views and the foreign keys that other tables rely on, and you would need to recreate them.

psql -X -d mydb -c '\d users'

2Fix

Drop the related tables together in one statement. When one DROP lists several tables, CASCADE is needed only for dependencies outside that group.

DROP TABLE orders, users;

3Verify

Recreate the tables from your schema file and check that the application starts.

psql -X -d mydb -c '\dt'

Confirmed by postgresql.org

Did this fix it?

How do I stop it coming back?

  • Name views and foreign keys in your migrations so DETAIL lines are readable.
  • Write down what CASCADE will remove (run the drop once without it) before using it in production.
  • Use migrations that drop dependents in reverse order of creation.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.