Add get_column_key
This commit is contained in:
@@ -4,6 +4,12 @@ Changelog
|
|||||||
Here you can see the full list of changes between each SQLAlchemy-Utils release.
|
Here you can see the full list of changes between each SQLAlchemy-Utils release.
|
||||||
|
|
||||||
|
|
||||||
|
0.26.5 (2014-07-xx)
|
||||||
|
^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
- Added get_column_key
|
||||||
|
|
||||||
|
|
||||||
0.26.4 (2014-06-25)
|
0.26.4 (2014-06-25)
|
||||||
^^^^^^^^^^^^^^^^^^^
|
^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
@@ -16,6 +16,12 @@ get_bind
|
|||||||
.. autofunction:: get_bind
|
.. autofunction:: get_bind
|
||||||
|
|
||||||
|
|
||||||
|
get_column_key
|
||||||
|
^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
.. autofunction:: get_column_key
|
||||||
|
|
||||||
|
|
||||||
get_columns
|
get_columns
|
||||||
^^^^^^^^^^^
|
^^^^^^^^^^^
|
||||||
|
|
||||||
|
2
setup.py
2
setup.py
@@ -44,7 +44,7 @@ for name, requirements in extras_require.items():
|
|||||||
|
|
||||||
setup(
|
setup(
|
||||||
name='SQLAlchemy-Utils',
|
name='SQLAlchemy-Utils',
|
||||||
version='0.26.4',
|
version='0.26.5',
|
||||||
url='https://github.com/kvesteri/sqlalchemy-utils',
|
url='https://github.com/kvesteri/sqlalchemy-utils',
|
||||||
license='BSD',
|
license='BSD',
|
||||||
author='Konsta Vesterinen, Ryan Leckey, Janne Vanhala, Vesa Uimonen',
|
author='Konsta Vesterinen, Ryan Leckey, Janne Vanhala, Vesa Uimonen',
|
||||||
|
@@ -12,6 +12,7 @@ from .functions import (
|
|||||||
drop_database,
|
drop_database,
|
||||||
escape_like,
|
escape_like,
|
||||||
get_bind,
|
get_bind,
|
||||||
|
get_column_key,
|
||||||
get_columns,
|
get_columns,
|
||||||
get_declarative_base,
|
get_declarative_base,
|
||||||
get_mapper,
|
get_mapper,
|
||||||
@@ -71,7 +72,7 @@ from .types import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
__version__ = '0.26.4'
|
__version__ = '0.26.5'
|
||||||
|
|
||||||
|
|
||||||
__all__ = (
|
__all__ = (
|
||||||
@@ -91,6 +92,7 @@ __all__ = (
|
|||||||
generates,
|
generates,
|
||||||
generic_relationship,
|
generic_relationship,
|
||||||
get_bind,
|
get_bind,
|
||||||
|
get_column_key,
|
||||||
get_columns,
|
get_columns,
|
||||||
get_declarative_base,
|
get_declarative_base,
|
||||||
get_mapper,
|
get_mapper,
|
||||||
|
@@ -20,6 +20,7 @@ from .foreign_keys import (
|
|||||||
)
|
)
|
||||||
from .orm import (
|
from .orm import (
|
||||||
get_bind,
|
get_bind,
|
||||||
|
get_column_key,
|
||||||
get_columns,
|
get_columns,
|
||||||
get_declarative_base,
|
get_declarative_base,
|
||||||
get_mapper,
|
get_mapper,
|
||||||
|
@@ -17,6 +17,33 @@ from sqlalchemy.orm.session import object_session
|
|||||||
from sqlalchemy.orm.util import AliasedInsp
|
from sqlalchemy.orm.util import AliasedInsp
|
||||||
|
|
||||||
|
|
||||||
|
def get_column_key(model, column):
|
||||||
|
"""
|
||||||
|
Return the key for given column in given model.
|
||||||
|
|
||||||
|
:param model: SQLAlchemy declarative model object
|
||||||
|
|
||||||
|
::
|
||||||
|
|
||||||
|
class User(Base):
|
||||||
|
__tablename__ = 'user'
|
||||||
|
id = sa.Column(sa.Integer, primary_key=True)
|
||||||
|
name = sa.Column('_name', sa.String)
|
||||||
|
|
||||||
|
|
||||||
|
get_column_key(User, User.__table__.c.name) # 'name'
|
||||||
|
|
||||||
|
.. versionadded: 0.26.5
|
||||||
|
"""
|
||||||
|
for key, c in sa.inspect(model).columns.items():
|
||||||
|
if c is column:
|
||||||
|
return key
|
||||||
|
raise ValueError(
|
||||||
|
"Class %s doesn't have a column 'column.key'",
|
||||||
|
model.__name__
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def get_mapper(mixed):
|
def get_mapper(mixed):
|
||||||
"""
|
"""
|
||||||
Return related SQLAlchemy Mapper for given SQLAlchemy object.
|
Return related SQLAlchemy Mapper for given SQLAlchemy object.
|
||||||
|
34
tests/functions/test_get_column_key.py
Normal file
34
tests/functions/test_get_column_key.py
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
from pytest import raises
|
||||||
|
|
||||||
|
import sqlalchemy as sa
|
||||||
|
from sqlalchemy.ext.declarative import declarative_base
|
||||||
|
|
||||||
|
from sqlalchemy_utils import get_column_key
|
||||||
|
|
||||||
|
|
||||||
|
class TestGetColumnKey(object):
|
||||||
|
def setup_method(self, method):
|
||||||
|
Base = declarative_base()
|
||||||
|
|
||||||
|
class Building(Base):
|
||||||
|
__tablename__ = 'building'
|
||||||
|
id = sa.Column(sa.Integer, primary_key=True)
|
||||||
|
name = sa.Column('_name', sa.Unicode(255))
|
||||||
|
|
||||||
|
self.Building = Building
|
||||||
|
|
||||||
|
def test_supports_aliases(self):
|
||||||
|
assert (
|
||||||
|
get_column_key(self.Building, self.Building.__table__.c.id)
|
||||||
|
==
|
||||||
|
'id'
|
||||||
|
)
|
||||||
|
assert (
|
||||||
|
get_column_key(self.Building, self.Building.__table__.c._name)
|
||||||
|
==
|
||||||
|
'name'
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_throws_value_error_for_unknown_column(self):
|
||||||
|
with raises(ValueError):
|
||||||
|
get_column_key(self.Building, 'unknown')
|
Reference in New Issue
Block a user