Add get_referencing_foreign_keys
This commit is contained in:
		@@ -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)
 | 
			
		||||
^^^^^^^^^^^^^^^^^^^
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -28,6 +28,12 @@ get_primary_keys
 | 
			
		||||
.. autofunction:: primary_keys
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
get_referencing_foreign_keys
 | 
			
		||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 | 
			
		||||
 | 
			
		||||
.. autofunction:: get_referencing_foreign_keys
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
query_entities
 | 
			
		||||
^^^^^^^^^^^^^^
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -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,
 | 
			
		||||
 
 | 
			
		||||
@@ -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',
 | 
			
		||||
 
 | 
			
		||||
@@ -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,
 | 
			
		||||
 
 | 
			
		||||
@@ -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()
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user