From 3aeb30ea104ca6cbe6972f7ec64233cadbaccd82 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Thu, 5 Jan 2023 10:34:37 -0500 Subject: warn and skip for FKs that refer to invisible cols for Oracle Supported use case for foreign key constraints where the local column is marked as "invisible". The errors normally generated when a :class:`.ForeignKeyConstraint` is created that check for the target column are disabled when reflecting, and the constraint is skipped with a warning in the same way which already occurs for an :class:`.Index` with a similar issue. tests are added for indexes, unique constraints, and primary key constraints, which were already working; indexes and uniques warn, primary keys don't which we would assume is because we never see those PK columns in the first place. Constraints now raise an informative ConstraintColumnNotFoundError in the general case for strings in the "pending colargs" collection not being resolvable. Fixes: #9059 Change-Id: I400cf0bff6abba0e0c75f38b07617be1a8ec3453 --- lib/sqlalchemy/sql/schema.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) (limited to 'lib/sqlalchemy/sql') diff --git a/lib/sqlalchemy/sql/schema.py b/lib/sqlalchemy/sql/schema.py index 62e962a32..5083940f0 100644 --- a/lib/sqlalchemy/sql/schema.py +++ b/lib/sqlalchemy/sql/schema.py @@ -4014,10 +4014,17 @@ class ColumnCollectionMixin: assert len(result) == len(self._pending_colargs) return result else: - return [ - parent.c[col] if isinstance(col, str) else col - for col in self._pending_colargs - ] + try: + return [ + parent.c[col] if isinstance(col, str) else col + for col in self._pending_colargs + ] + except KeyError as ke: + raise exc.ConstraintColumnNotFoundError( + f"Can't create {self.__class__.__name__} " + f"on table '{parent.description}': no column " + f"named '{ke.args[0]}' is present." + ) from ke def _set_parent(self, parent: SchemaEventTarget, **kw: Any) -> None: assert isinstance(parent, (Table, Column)) @@ -4519,14 +4526,7 @@ class ForeignKeyConstraint(ColumnCollectionConstraint): assert isinstance(table, Table) Constraint._set_parent(self, table) - try: - ColumnCollectionConstraint._set_parent(self, table) - except KeyError as ke: - raise exc.ArgumentError( - "Can't create ForeignKeyConstraint " - "on table '%s': no column " - "named '%s' is present." % (table.description, ke.args[0]) - ) from ke + ColumnCollectionConstraint._set_parent(self, table) for col, fk in zip(self._columns, self.elements): if not hasattr(fk, "parent") or fk.parent is not col: -- cgit v1.2.1