defaults /sequences assigned to both python side and server side

Issue #3676 new
Mike Bayer repo owner created an issue
from sqlalchemy import create_engine, MetaData, Sequence, Integer, Column
from sqlalchemy.ext.declarative import declarative_base

engine = create_engine('postgresql://scott:tiger@localhost/test', echo=True)
metadata = MetaData(bind=engine)
Base = declarative_base(metadata=metadata)

id_sequence = Sequence('drop_seq_col')


class Station(Base):
    __tablename__ = 'drop_seq_test'

    col = Column(Integer, id_sequence, server_default=id_sequence.next_value())
    source_id = Column(Integer, primary_key=True)


metadata.create_all()
metadata.drop_all()

the above will fail to drop unless we move the per-table drop of "column.default" to be after the table:

diff --git a/lib/sqlalchemy/sql/ddl.py b/lib/sqlalchemy/sql/ddl.py
index 48f27b8..12b08a2 100644
--- a/lib/sqlalchemy/sql/ddl.py
+++ b/lib/sqlalchemy/sql/ddl.py
@@ -916,12 +916,12 @@ class SchemaDropper(DDLBase):
             _ddl_runner=self,
             _is_metadata_operation=_is_metadata_operation)

+        self.connection.execute(DropTable(table))
+
         for column in table.columns:
             if column.default is not None:
                 self.traverse_single(column.default)

-        self.connection.execute(DropTable(table))
-
         table.dispatch.after_drop(
             table, self.connection,
            checkfirst=self.checkfirst,

however, the user is associating the same Sequence both with the python side default as well as the server side default, where using just one or the other would suffice. As it is, the arrangement is confusing that a python-side default implicitly creates /drops a sequence but a server-side default does not - the server side default should probably create the sequence as well and additionally any column that has both python-side and server-side defaults specified should emit a warning (or would someone intentionally want both?).

perhaps the whole thing would have been clearer if a Column had just one "default" attribute, and client vs. server vs. both! could be determined from the object.

Comments (1)

  1. Mike Bayer reporter
    • changed milestone to 1.3
    • edited description

    this is an odd use case so this will move out until we have time to get to it

  2. Log in to comment