diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2011-02-17 20:27:35 -0500 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2011-02-17 20:27:35 -0500 |
commit | 01cb94690d2d07431b2ff1b02577345c003be42b (patch) | |
tree | 617bb8748e61c05bebcfacebcd3856f4c0e95d85 | |
parent | 7b22358fab6a178a4183769338beed32c69ea5be (diff) | |
download | sqlalchemy-01cb94690d2d07431b2ff1b02577345c003be42b.tar.gz |
- The Column.references() method now returns True
if it has a foreign key referencing the
given column exactly, not just it's parent
table. [ticket:2064]
-rw-r--r-- | CHANGES | 5 | ||||
-rw-r--r-- | lib/sqlalchemy/schema.py | 2 | ||||
-rw-r--r-- | test/sql/test_metadata.py | 46 |
3 files changed, 52 insertions, 1 deletions
@@ -29,6 +29,11 @@ CHANGES metadata.create_all() and metadata.drop_all(), including "checkfirst" logic. [ticket:2055] + - The Column.references() method now returns True + if it has a foreign key referencing the + given column exactly, not just it's parent + table. [ticket:2064] + - ext - Association proxy now has correct behavior for any(), has(), and contains() when proxying diff --git a/lib/sqlalchemy/schema.py b/lib/sqlalchemy/schema.py index 40d7de945..e6b970291 100644 --- a/lib/sqlalchemy/schema.py +++ b/lib/sqlalchemy/schema.py @@ -766,7 +766,7 @@ class Column(SchemaItem, expression.ColumnClause): key.""" for fk in self.foreign_keys: - if fk.references(column.table): + if fk.column.proxy_set.intersection(column.proxy_set): return True else: return False diff --git a/test/sql/test_metadata.py b/test/sql/test_metadata.py index 38788330a..ace3b69d5 100644 --- a/test/sql/test_metadata.py +++ b/test/sql/test_metadata.py @@ -500,6 +500,52 @@ class TableTest(TestBase, AssertsCompiledSQL): assign ) +class ConstraintTest(TestBase): + def _single_fixture(self): + m = MetaData() + + t1 = Table('t1', m, + Column('a', Integer), + Column('b', Integer) + ) + + t2 = Table('t2', m, + Column('a', Integer, ForeignKey('t1.a')) + ) + + t3 = Table('t3', m, + Column('a', Integer) + ) + return t1, t2, t3 + + def test_table_references(self): + t1, t2, t3 = self._single_fixture() + assert list(t2.c.a.foreign_keys)[0].references(t1) + assert not list(t2.c.a.foreign_keys)[0].references(t3) + + def test_column_references(self): + t1, t2, t3 = self._single_fixture() + assert t2.c.a.references(t1.c.a) + assert not t2.c.a.references(t3.c.a) + assert not t2.c.a.references(t1.c.b) + + def test_column_references_derived(self): + t1, t2, t3 = self._single_fixture() + s1 = tsa.select([tsa.select([t1]).alias()]) + assert t2.c.a.references(s1.c.a) + assert not t2.c.a.references(s1.c.b) + + def test_copy_doesnt_reference(self): + t1, t2, t3 = self._single_fixture() + a2 = t2.c.a.copy() + assert not a2.references(t1.c.a) + assert not a2.references(t1.c.b) + + def test_derived_column_references(self): + t1, t2, t3 = self._single_fixture() + s1 = tsa.select([tsa.select([t2]).alias()]) + assert s1.c.a.references(t1.c.a) + assert not s1.c.a.references(t1.c.b) class ColumnDefinitionTest(TestBase): """Test Column() construction.""" |