def _add_fk(obj, compare_to):
if _run_filters(
- obj.const, obj.name, "foreignkey", False,
+ obj.const, obj.name, "foreign_key_constraint", False,
compare_to, object_filters):
diffs.append(('add_fk', const.const))
def _remove_fk(obj, compare_to):
if _run_filters(
- obj.const, obj.name, "foreignkey", True,
+ obj.const, obj.name, "foreign_key_constraint", True,
compare_to, object_filters):
diffs.append(('remove_fk', obj.const))
log.info(
as a :class:`~sqlalchemy.schema.Table`,
:class:`~sqlalchemy.schema.Column`,
:class:`~sqlalchemy.schema.Index`
- or :class:`~sqlalchemy.schema.UniqueConstraint` object
+ :class:`~sqlalchemy.schema.UniqueConstraint`,
+ or :class:`~sqlalchemy.schema.ForeignKeyConstraint` object
* ``name``: the name of the object. This is typically available
via ``object.name``.
* ``type``: a string describing the type of object; currently
- ``"table"``, ``"column"``, ``"index"`` or ``"unique_constraint"``.
+ ``"table"``, ``"column"``, ``"index"``, ``"unique_constraint"``,
+ or ``"foreign_key_constraint"``
.. versionadded:: 0.7.0 Support for indexes and unique constraints
within the
:paramref:`~.EnvironmentContext.configure.include_object` hook.
+ .. versionadded:: 0.7.1 Support for foreign keys within the
+ :paramref:`~.EnvironmentContext.configure.include_object` hook.
+
* ``reflected``: ``True`` if the given object was produced based on
table reflection, ``False`` if it's from a local :class:`.MetaData`
object.
.. versionadded:: 0.6.1 Support for autogenerate of indexes and unique constraints.
+* Basic changes in foreign key constraints
+
+.. versionadded:: 0.7.1 Support for autogenerate of foreign key constraints.
+
Autogenerate can **optionally detect**:
* Change of column type. This will occur if you set
Autogenerate can't currently, but **will eventually detect**:
* Some free-standing constraint additions and removals,
- like CHECK, FOREIGN KEY, PRIMARY KEY - these are not fully implemented.
+ like CHECK, PRIMARY KEY - these are not fully implemented.
* Sequence additions, removals - not yet implemented.
.. changelog::
:version: 0.7.1
+ .. change::
+ :tags: feature, autogenerate
+ :tickets: 178
+ :pullreq: bitbucket:32
+
+ Support for autogenerate of FOREIGN KEY constraints has been added.
+ These are delivered within the autogenerate process in the same
+ manner as UNIQUE constraints, including ``include_object`` support.
+ Big thanks to Ann Kamyshnikova for doing the heavy lifting here.
+
.. change::
:tags: bug, batch
:pullreq: bitbucket:34
def include_object(object_, name, type_, reflected, compare_to):
return not (
isinstance(object_, ForeignKeyConstraint) and
- type_ == 'foreignkey' and reflected and name == 'fk1')
+ type_ == 'foreign_key_constraint'
+ and reflected and name == 'fk1')
diffs = self._fixture(m1, m2, object_filters=[include_object])
def include_object(object_, name, type_, reflected, compare_to):
return not (
isinstance(object_, ForeignKeyConstraint) and
- type_ == 'foreignkey' and not reflected and name == 'fk1')
+ type_ == 'foreign_key_constraint'
+ and not reflected and name == 'fk1')
diffs = self._fixture(m1, m2, object_filters=[include_object])
def include_object(object_, name, type_, reflected, compare_to):
return not (
isinstance(object_, ForeignKeyConstraint) and
- type_ == 'foreignkey' and name == 'fk1'
+ type_ == 'foreign_key_constraint'
+ and name == 'fk1'
)
diffs = self._fixture(m1, m2, object_filters=[include_object])
"['code'], schema='CamelSchema')"
)
- def test_drop_constraint(self):
+ def test_drop_unique_constraint(self):
"""
autogenerate.render._drop_constraint
"""
uq = UniqueConstraint(t.c.code, name='uq_test_code')
eq_ignore_whitespace(
autogenerate.render._drop_constraint(uq, self.autogen_context),
- "op.drop_constraint('uq_test_code', 'test')"
+ "op.drop_constraint('uq_test_code', 'test', type_='unique')"
)
- def test_drop_constraint_schema(self):
+ def test_drop_unique_constraint_schema(self):
"""
autogenerate.render._drop_constraint using schema
"""
uq = UniqueConstraint(t.c.code, name='uq_test_code')
eq_ignore_whitespace(
autogenerate.render._drop_constraint(uq, self.autogen_context),
- "op.drop_constraint('uq_test_code', 'test', schema='CamelSchema')"
+ "op.drop_constraint('uq_test_code', 'test', "
+ "schema='CamelSchema', type_='unique')"
+ )
+
+ def test_drop_fk_constraint(self):
+ m = MetaData()
+ Table('a', m, Column('id', Integer, primary_key=True))
+ b = Table('b', m, Column('a_id', Integer, ForeignKey('a.id')))
+ fk = ForeignKeyConstraint(['a_id'], ['a.id'], name='fk_a_id')
+ b.append_constraint(fk)
+ eq_ignore_whitespace(
+ autogenerate.render._drop_constraint(fk, self.autogen_context),
+ "op.drop_constraint('fk_a_id', 'b', type_='foreignkey')"
+ )
+
+ def test_drop_fk_constraint_schema(self):
+ m = MetaData()
+ m = MetaData()
+ Table(
+ 'a', m, Column('id', Integer, primary_key=True),
+ schema="CamelSchemaTwo")
+ b = Table(
+ 'b', m, Column('a_id', Integer, ForeignKey('a.id')),
+ schema="CamelSchemaOne")
+ fk = ForeignKeyConstraint(
+ ["a_id"],
+ ["CamelSchemaTwo.a.id"], name='fk_a_id')
+ b.append_constraint(fk)
+
+ eq_ignore_whitespace(
+ autogenerate.render._drop_constraint(fk, self.autogen_context),
+ "op.drop_constraint('fk_a_id', 'b', schema='CamelSchemaOne', "
+ "type_='foreignkey')"
)
def test_render_table_upgrade(self):