From 851a14aa1a0e7a734a6f810f0e6e5c39d8e63b1b Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Wed, 3 Dec 2008 14:09:34 +0000 Subject: [PATCH] - Using the same ForeignKey object repeatedly raises an error instead of silently failing later. [ticket:1238] --- CHANGES | 4 ++++ lib/sqlalchemy/schema.py | 2 ++ test/sql/constraints.py | 11 ++++++++++- 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/CHANGES b/CHANGES index ca1cf87803..562a5baa8f 100644 --- a/CHANGES +++ b/CHANGES @@ -65,6 +65,10 @@ CHANGES - sql - Fixed the import weirdness in sqlalchemy.sql to not export __names__ [ticket:1215]. + + - Using the same ForeignKey object repeatedly + raises an error instead of silently failing + later. [ticket:1238] - Added NotImplementedError for params() method on Insert/Update/Delete constructs. These items diff --git a/lib/sqlalchemy/schema.py b/lib/sqlalchemy/schema.py index aad1199060..e9e49ba0ed 100644 --- a/lib/sqlalchemy/schema.py +++ b/lib/sqlalchemy/schema.py @@ -872,6 +872,8 @@ class ForeignKey(SchemaItem): return _column def _set_parent(self, column): + if hasattr(self, 'parent'): + raise exc.InvalidRequestError("This ForeignKey already has a parent !") self.parent = column if self.parent._pre_existing_column is not None: diff --git a/test/sql/constraints.py b/test/sql/constraints.py index b7208532c5..ab08c6d981 100644 --- a/test/sql/constraints.py +++ b/test/sql/constraints.py @@ -29,7 +29,16 @@ class ConstraintTest(TestBase, AssertsExecutionResults): ForeignKeyConstraint(['emp_id', 'emp_soc'], ['employees.id', 'employees.soc']) ) metadata.create_all() - + + def test_double_fk_usage_raises(self): + f = ForeignKey('b.id') + + self.assertRaises(exc.InvalidRequestError, Table, "a", metadata, + Column('x', Integer, f), + Column('y', Integer, f) + ) + + def test_circular_constraint(self): a = Table("a", metadata, Column('id', Integer, primary_key=True), -- 2.47.3