from sqlalchemy import Table, MetaData, Index, select, Column, \
ForeignKeyConstraint, cast
from sqlalchemy import types as sqltypes
+from sqlalchemy import schema as sql_schema
from sqlalchemy.util import OrderedDict
from . import util
from .ddl.base import _columns_for_constraint, _is_type_bound
else:
referent_schema = None
if tname != '_alembic_batch_temp':
- Table(
- tname, metadata,
- *[Column(n, sqltypes.NULLTYPE) for n in
- [elem._get_colspec().split(".")[-1]
- for elem in constraint.elements]],
- schema=referent_schema)
+ key = sql_schema._get_table_key(tname, referent_schema)
+ if key in metadata.tables:
+ t = metadata.tables[key]
+ for elem in constraint.elements:
+ colname = elem._get_colspec().split(".")[-1]
+ if not t.c.contains_column(colname):
+ t.append_column(
+ Column(colname, sqltypes.NULLTYPE)
+ )
+ else:
+ Table(
+ tname, metadata,
+ *[Column(n, sqltypes.NULLTYPE) for n in
+ [elem._get_colspec().split(".")[-1]
+ for elem in constraint.elements]],
+ schema=referent_schema)
def _create(self, op_impl):
self._transfer_elements_to_new_table()
)
return ApplyBatchImpl(t, table_args, table_kwargs)
+ def _multi_fk_fixture(self, table_args=(), table_kwargs={}):
+ m = MetaData()
+ t = Table(
+ 'tname', m,
+ Column('id', Integer, primary_key=True),
+ Column('email', String()),
+ Column('user_id_1', Integer, ForeignKey('user.id')),
+ Column('user_id_2', Integer, ForeignKey('user.id')),
+ Column('user_id_3', Integer),
+ Column('user_id_version', Integer),
+ ForeignKeyConstraint(
+ ['user_id_3', 'user_id_version'],
+ ['user.id', 'user.id_version'])
+ )
+ return ApplyBatchImpl(t, table_args, table_kwargs)
+
def _named_fk_fixture(self, table_args=(), table_kwargs={}):
m = MetaData()
t = Table(
"user.id"
)
+ def test_regen_multi_fk(self):
+ impl = self._multi_fk_fixture()
+ self._assert_impl(
+ impl, colnames=[
+ 'id', 'email', 'user_id_1', 'user_id_2',
+ 'user_id_3', 'user_id_version'],
+ ddl_contains='FOREIGN KEY(user_id_3, user_id_version) '
+ 'REFERENCES "user" (id, id_version)')
+
def test_drop_col(self):
impl = self._simple_fixture()
impl.drop_column('tname', column('x'))