if util.sqla_08:
from sqlalchemy.events import SchemaEventTarget
from ..util.sqla_compat import _columns_for_constraint, \
- _is_type_bound, _fk_is_self_referential
+ _is_type_bound, _fk_is_self_referential, _remove_column_from_collection
class BatchOperationsImpl(object):
self.column_transfers[column.name] = {}
def drop_column(self, table_name, column, **kw):
+ if column.name in self.table.primary_key.columns:
+ _remove_column_from_collection(
+ self.table.primary_key.columns,
+ column
+ )
del self.columns[column.name]
del self.column_transfers[column.name]
class BatchRoundTripTest(TestBase):
- __requires__ = ('sqlalchemy_08', )
+ __requires__ = ('sqlalchemy_09', )
__only_on__ = "sqlite"
def setUp(self):
{"id": 5, "x": 9}
])
+ def test_drop_pk_col_readd_col(self):
+ # drop a column, add it back without primary_key=True, should no
+ # longer be in the constraint
+ with self.op.batch_alter_table("foo") as batch_op:
+ batch_op.drop_column('id')
+ batch_op.add_column(Column('id', Integer))
+
+ pk_const = Inspector.from_engine(self.conn).get_pk_constraint('foo')
+ eq_(pk_const['constrained_columns'], [])
+
+ def test_drop_pk_col_readd_pk_col(self):
+ # drop a column, add it back with primary_key=True, should remain
+ with self.op.batch_alter_table("foo") as batch_op:
+ batch_op.drop_column('id')
+ batch_op.add_column(Column('id', Integer, primary_key=True))
+
+ pk_const = Inspector.from_engine(self.conn).get_pk_constraint('foo')
+ eq_(pk_const['constrained_columns'], ['id'])
+
+ def test_drop_pk_col_readd_col_also_pk_const(self):
+ # drop a column, add it back without primary_key=True, but then
+ # also make anew PK constraint that includes it, should remain
+ with self.op.batch_alter_table("foo") as batch_op:
+ batch_op.drop_column('id')
+ batch_op.add_column(Column('id', Integer))
+ batch_op.create_primary_key('newpk', ['id'])
+
+ pk_const = Inspector.from_engine(self.conn).get_pk_constraint('foo')
+ eq_(pk_const['constrained_columns'], ['id'])
+
def test_add_pk_constraint(self):
self._no_pk_fixture()
with self.op.batch_alter_table("nopk", recreate="always") as batch_op:
class BatchRoundTripMySQLTest(BatchRoundTripTest):
__only_on__ = "mysql"
+ @exclusions.fails()
+ def test_drop_pk_col_readd_pk_col(self):
+ super(BatchRoundTripMySQLTest, self).test_drop_pk_col_readd_pk_col()
+
+ @exclusions.fails()
+ def test_drop_pk_col_readd_col_also_pk_const(self):
+ super(
+ BatchRoundTripMySQLTest, self
+ ).test_drop_pk_col_readd_col_also_pk_const()
+
@exclusions.fails()
def test_rename_column_pk(self):
super(BatchRoundTripMySQLTest, self).test_rename_column_pk()
class BatchRoundTripPostgresqlTest(BatchRoundTripTest):
__only_on__ = "postgresql"
+ @exclusions.fails()
+ def test_drop_pk_col_readd_pk_col(self):
+ super(
+ BatchRoundTripPostgresqlTest, self).test_drop_pk_col_readd_pk_col()
+
+ @exclusions.fails()
+ def test_drop_pk_col_readd_col_also_pk_const(self):
+ super(
+ BatchRoundTripPostgresqlTest, self
+ ).test_drop_pk_col_readd_col_also_pk_const()
+
@exclusions.fails()
def test_change_type(self):
super(BatchRoundTripPostgresqlTest, self).test_change_type()