Added table_name utility function
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.13.0 (2013-06-05)
|
||||||
|
^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
- Added table_name utility function.
|
||||||
|
|
||||||
|
|
||||||
0.12.5 (2013-06-05)
|
0.12.5 (2013-06-05)
|
||||||
^^^^^^^^^^^^^^^^^^^
|
^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
2
setup.py
2
setup.py
@@ -24,7 +24,7 @@ class PyTest(Command):
|
|||||||
|
|
||||||
setup(
|
setup(
|
||||||
name='SQLAlchemy-Utils',
|
name='SQLAlchemy-Utils',
|
||||||
version='0.12.5',
|
version='0.13.0',
|
||||||
url='https://github.com/kvesteri/sqlalchemy-utils',
|
url='https://github.com/kvesteri/sqlalchemy-utils',
|
||||||
license='BSD',
|
license='BSD',
|
||||||
author='Konsta Vesterinen',
|
author='Konsta Vesterinen',
|
||||||
|
@@ -1,4 +1,6 @@
|
|||||||
from .functions import sort_query, defer_except, escape_like, primary_keys
|
from .functions import (
|
||||||
|
sort_query, defer_except, escape_like, primary_keys, table_name
|
||||||
|
)
|
||||||
from .listeners import coercion_listener
|
from .listeners import coercion_listener
|
||||||
from .merge import merge, Merger
|
from .merge import merge, Merger
|
||||||
from .proxy_dict import ProxyDict, proxy_dict
|
from .proxy_dict import ProxyDict, proxy_dict
|
||||||
@@ -27,6 +29,7 @@ __all__ = (
|
|||||||
merge,
|
merge,
|
||||||
primary_keys,
|
primary_keys,
|
||||||
proxy_dict,
|
proxy_dict,
|
||||||
|
table_name,
|
||||||
ColorType,
|
ColorType,
|
||||||
EmailType,
|
EmailType,
|
||||||
InstrumentedList,
|
InstrumentedList,
|
||||||
|
@@ -201,3 +201,13 @@ def primary_keys(class_):
|
|||||||
for column in class_.__table__.c:
|
for column in class_.__table__.c:
|
||||||
if column.primary_key:
|
if column.primary_key:
|
||||||
yield column
|
yield column
|
||||||
|
|
||||||
|
|
||||||
|
def table_name(class_):
|
||||||
|
"""
|
||||||
|
Return table name of given declarative class.
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
return class_.__tablename__
|
||||||
|
except AttributeError:
|
||||||
|
return class_.__table__.name
|
||||||
|
18
tests/test_table_name.py
Normal file
18
tests/test_table_name.py
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
import sqlalchemy as sa
|
||||||
|
from sqlalchemy_utils import table_name
|
||||||
|
from tests import DatabaseTestCase
|
||||||
|
|
||||||
|
|
||||||
|
class TestTableName(DatabaseTestCase):
|
||||||
|
def create_models(self):
|
||||||
|
class Building(self.Base):
|
||||||
|
__tablename__ = 'building'
|
||||||
|
id = sa.Column(sa.Integer, primary_key=True)
|
||||||
|
name = sa.Column(sa.Unicode(255))
|
||||||
|
|
||||||
|
self.Building = Building
|
||||||
|
|
||||||
|
def test_table_name(self):
|
||||||
|
assert table_name(self.Building) == 'building'
|
||||||
|
del self.Building.__tablename__
|
||||||
|
assert table_name(self.Building) == 'building'
|
Reference in New Issue
Block a user