From 88690ea83e8670b6668790d8bbbf4b58f96dd51b Mon Sep 17 00:00:00 2001 From: Konsta Vesterinen Date: Mon, 28 Apr 2014 15:56:23 +0300 Subject: [PATCH] Add get_referencing_foreign_keys --- CHANGES.rst | 6 ++++++ docs/model_helpers.rst | 6 ++++++ sqlalchemy_utils/__init__.py | 2 ++ sqlalchemy_utils/functions/__init__.py | 2 ++ sqlalchemy_utils/functions/orm.py | 29 ++++++++++++++++++++++++++ tests/__init__.py | 2 +- 6 files changed, 46 insertions(+), 1 deletion(-) diff --git a/CHANGES.rst b/CHANGES.rst index dbdc706..02473d0 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -4,6 +4,12 @@ Changelog Here you can see the full list of changes between each SQLAlchemy-Utils release. +0.25.5 (2014-xx-xx) +^^^^^^^^^^^^^^^^^^^ + +- Added get_referencing_foreign_keys + + 0.25.4 (2014-04-22) ^^^^^^^^^^^^^^^^^^^ diff --git a/docs/model_helpers.rst b/docs/model_helpers.rst index 4a4df52..a06b6e1 100644 --- a/docs/model_helpers.rst +++ b/docs/model_helpers.rst @@ -28,6 +28,12 @@ get_primary_keys .. autofunction:: primary_keys +get_referencing_foreign_keys +^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +.. autofunction:: get_referencing_foreign_keys + + query_entities ^^^^^^^^^^^^^^ diff --git a/sqlalchemy_utils/__init__.py b/sqlalchemy_utils/__init__.py index 0a4354c..2589a29 100644 --- a/sqlalchemy_utils/__init__.py +++ b/sqlalchemy_utils/__init__.py @@ -13,6 +13,7 @@ from .functions import ( get_columns, get_declarative_base, get_primary_keys, + get_referencing_foreign_keys, identity, mock_engine, naturally_equivalent, @@ -80,6 +81,7 @@ __all__ = ( get_columns, get_declarative_base, get_primary_keys, + get_referencing_foreign_keys, identity, instrumented_list, merge, diff --git a/sqlalchemy_utils/functions/__init__.py b/sqlalchemy_utils/functions/__init__.py index b414d5c..6f35ef2 100644 --- a/sqlalchemy_utils/functions/__init__.py +++ b/sqlalchemy_utils/functions/__init__.py @@ -15,6 +15,7 @@ from .orm import ( get_columns, get_declarative_base, get_primary_keys, + get_referencing_foreign_keys, getdotattr, has_changes, identity, @@ -33,6 +34,7 @@ __all__ = ( 'get_columns', 'get_declarative_base', 'get_primary_keys', + 'get_referencing_foreign_keys', 'getdotattr', 'has_changes', 'identity', diff --git a/sqlalchemy_utils/functions/orm.py b/sqlalchemy_utils/functions/orm.py index eb4febe..5d84435 100644 --- a/sqlalchemy_utils/functions/orm.py +++ b/sqlalchemy_utils/functions/orm.py @@ -14,6 +14,35 @@ from sqlalchemy.orm.query import _ColumnEntity from sqlalchemy.orm.util import AliasedInsp +def get_referencing_foreign_keys(mixed): + """ + Returns referencing foreign keys for given Table object or declarative + class. + + :param mixed: + SA Table object or SA declarative class + + :: + + get_foreign_keys(User) # set([ForeignKey('user.id')]) + """ + if isinstance(mixed, sa.Table): + tables = [mixed] + else: + # TODO: make this support joined table inheritance + tables = [mixed.__table__] + + referencing_foreign_keys = set() + + for table in mixed.metadata.tables.values(): + for constraint in table.constraints: + if isinstance(constraint, sa.sql.schema.ForeignKeyConstraint): + for fk in constraint.elements: + if any(fk.references(t) for t in tables): + referencing_foreign_keys.add(fk) + return referencing_foreign_keys + + def get_primary_keys(mixed): """ Return an OrderedDict of all primary keys for given Table object, diff --git a/tests/__init__.py b/tests/__init__.py index b4faa10..6935828 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -38,7 +38,7 @@ class TestCase(object): def setup_method(self, method): self.engine = create_engine(self.dns) - self.engine.echo = True + # self.engine.echo = True self.connection = self.engine.connect() self.Base = declarative_base()