Make MetaData.drop_all() work with foreign keys

Issue #3569 closed
Roman Z. created an issue

Currently issuing a drop_all() for PostgreSQL databases sometimes leads to a foreign key error like:

sqlalchemy.exc.InternalError: (psycopg2.InternalError) cannot drop table users because other objects depend on it
DETAIL:  constraint engaging_networks_tokens_user_id_fkey on table engaging_networks_tokens depends on table users
HINT:  Use DROP ... CASCADE to drop the dependent objects too.
 [SQL: '\nDROP TABLE users']

This is a major nuissance since drop_all() is commonly used for testing.

A workaround is documented in this wiki entry.

Comments (2)

  1. Roman Z. reporter
    • marked as bug
    • changed component to (none)
    • edited description

    There seems to be code that "should" handle this in sqlalchemy.sql.ddl.SchemaDropper but it somehow seems to fail. It seems that this supposed to work at some point. So I promote this to a bug-report.

  2. Roman Z. reporter

    Ok seems that this happens exactly when one table is not part of the current sqlalchemy metadata. So I guess everything is fine with sqlalchemy itself.

  3. Log in to comment