Extend table_name to support attributes.

This commit is contained in:
Ryan Leckey
2013-09-09 00:35:31 -07:00
parent a6169a553c
commit 684617f875
2 changed files with 18 additions and 3 deletions

View File

@@ -90,14 +90,22 @@ def primary_keys(class_):
yield column
def table_name(class_):
def table_name(obj):
"""
Return table name of given declarative class.
Return table name of given target, declarative class or the
table name where the declarative attribute is bound to.
"""
class_ = getattr(obj, 'class_', obj)
try:
return class_.__tablename__
except AttributeError:
pass
try:
return class_.__table__.name
except AttributeError:
pass
def non_indexed_foreign_keys(metadata, engine=None):

View File

@@ -12,7 +12,14 @@ class TestTableName(TestCase):
self.Building = Building
def test_table_name(self):
def test_class(self):
assert table_name(self.Building) == 'building'
del self.Building.__tablename__
assert table_name(self.Building) == 'building'
def test_attribute(self):
assert table_name(self.Building.id) == 'building'
assert table_name(self.Building.name) == 'building'
def test_target(self):
assert table_name(self.Building()) == 'building'