Added table_name utility function

This commit is contained in:
Konsta Vesterinen
2013-06-10 10:05:53 +03:00
parent e181292bb5
commit 814855acf9
5 changed files with 39 additions and 2 deletions

View File

@@ -4,6 +4,12 @@ Changelog
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)
^^^^^^^^^^^^^^^^^^^

View File

@@ -24,7 +24,7 @@ class PyTest(Command):
setup(
name='SQLAlchemy-Utils',
version='0.12.5',
version='0.13.0',
url='https://github.com/kvesteri/sqlalchemy-utils',
license='BSD',
author='Konsta Vesterinen',

View File

@@ -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 .merge import merge, Merger
from .proxy_dict import ProxyDict, proxy_dict
@@ -27,6 +29,7 @@ __all__ = (
merge,
primary_keys,
proxy_dict,
table_name,
ColorType,
EmailType,
InstrumentedList,

View File

@@ -201,3 +201,13 @@ def primary_keys(class_):
for column in class_.__table__.c:
if column.primary_key:
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
View 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'