[Sqlalchemy-tickets] Issue #4300: Column defaults are dropped before the table itself (zzzeek/sqlal
Brought to you by:
zzzeek
From: Quentin G. (Sandhose) <iss...@bi...> - 2018-07-10 11:59:11
|
New issue 4300: Column defaults are dropped before the table itself https://bitbucket.org/zzzeek/sqlalchemy/issues/4300/column-defaults-are-dropped-before-the Quentin Gliech (Sandhose): When dropping a table, it first tries to drop the object associated with the `default` parameter of each column before dropping the table itself. The documentation gives [an example](http://docs.sqlalchemy.org/en/latest/core/defaults.html#associating-a-sequence-as-the-server-side-default) with a sequence that is used as a `default` and a `server_default`. In this particular example, the table will fail to drop with postgres because it first try to drop the sequence, which fails because it is still associated with the column. Here is a full example that illustrates this bug: ``` from sqlalchemy import create_engine, Table, Column, Sequence, Integer from sqlalchemy.schema import MetaData engine = create_engine('postgres+psycopg2://localhost/', echo=True) metadata = MetaData() table_id_seq = Sequence('table_id_seq', metadata=metadata) table = Table( 'table', metadata, Column( name='id', type_=Integer, default=table_id_seq, server_default=table_id_seq.next_value(), primary_key=True ) ) metadata.create_all(bind=engine) metadata.drop_all(bind=engine) # will fail # sqlalchemy.exc.InternalError: (psycopg2.InternalError) cannot drop sequence table_id_seq because other objects depend on it ``` This particular example runs with postgres using the psycopg2 driver, but other dialects might be affected by this. The full log and stacktrace is in the attachments. --- Looking at the code responsible of creating and dropping the tables, even though it seems like the [sequences are treated separately](https://bitbucket.org/zzzeek/sqlalchemy/src/260c604942bbdda1e4fbed951c2f591b825932e6/lib/sqlalchemy/sql/ddl.py#lines-903:912) and dropped after the table is dropped, they are dropped before because [the `default` field of each column is dropped](https://bitbucket.org/zzzeek/sqlalchemy/src/260c604942bbdda1e4fbed951c2f591b825932e6/lib/sqlalchemy/sql/ddl.py#lines-951:955) when visiting the table. --- ``` macOS 10.13.5 python 3.6.5 postgresql 10.4 SQLAlchemy 1.2.9 psycopg2-binary 2.7.5 ``` |