DROP TABLE is refused because a view or foreign key points at it
ERROR: cannot drop table users because other objects depend on itShort 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.
Fix it fast
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;2Match what you see, then jump to its fix
- 1
DETAIL: constraint orders_user_id_fkey on table orders depends on table usersAnother table has a foreign key to this tableFix - 2
DETAIL: view active_users depends on table usersA view reads from the tableFix - 3
The goal is to reload the data, not to remove the table for good.You are rebuilding the table and want to keep its dependentsFix
- 1
What does the error look like?
ERROR: cannot drop table users because other objects depend on itHINT: 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
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
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
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
Databases
DROP ROLE refuses because the role still owns objects or has default privileges
DETAIL: owner of table bobs_table
DROP ROLE fails while the role is referenced anywhere in the cluster.
4 causes, each with a fixDatabases
DBeaver cannot run pg_restore: the utility is not found in the client home
Utility 'pg_restore' not found in client home '/usr/lib' (/usr/lib)
DBeaver runs pg_restore from a local PostgreSQL client folder; it does not bundle one.
3 causes, each with a fixDatabases
DBeaver cannot download the PostgreSQL driver from Maven
The PostgreSQL driver files are missing and the download does not complete
DBeaver downloads the PostgreSQL JDBC driver from a Maven repository the first time you connect.
3 causes, each with a fixDatabases
python manage.py migrate finishes but the tables for my app are missing
relation "myapp_book" does not exist
migrate only applies migration files that exist.
3 causes, each with a fixDatabases
Django save fails on PostgreSQL: null value in a column violates a not-null constraint
django.db.utils.IntegrityError: null value in column "price" of relation "shop_product" violates not-null constraint
A model field is NOT NULL in the database unless it has null=True.
3 causes, each with a fixDatabases
Django migration on PostgreSQL stops with 'pending trigger events'
django.db.utils.OperationalError: cannot ALTER TABLE "mytable" because it has pending trigger events
PostgreSQL runs a Django migration in one transaction.
2 causes, each with a fix