Skip to content

Commit 3ccdae4

Browse files
committed
Fixed missing table argument for indicating the table's schema (fixes agronholm#2)
1 parent 6e54cbb commit 3ccdae4

2 files changed

Lines changed: 93 additions & 2 deletions

File tree

sqlacodegen/codegen.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ class Model(object):
166166
def __init__(self, table):
167167
super(Model, self).__init__()
168168
self.table = table
169+
self.schema = table.schema
169170

170171
# Adapt column types to the most reasonable generic types (ie. VARCHAR -> String)
171172
for column in table.columns:
@@ -224,6 +225,9 @@ def render(self):
224225
if len(index.columns) > 1:
225226
text += ' {0},\n'.format(_render_index(index))
226227

228+
if self.schema:
229+
text += " schema='{0}',\n".format(self.schema)
230+
227231
return text.rstrip('\n,') + '\n)'
228232

229233

@@ -293,7 +297,18 @@ def render(self):
293297
for index in self.table.indexes:
294298
if len(index.columns) > 1:
295299
table_args.append(_render_index(index))
296-
if table_args:
300+
301+
table_kwargs = {}
302+
if self.schema:
303+
table_kwargs['schema'] = self.schema
304+
305+
kwargs_items = ', '.join('{0!r}: {1!r}'.format(key, table_kwargs[key]) for key in table_kwargs)
306+
kwargs_items = '{{{0}}}'.format(kwargs_items) if kwargs_items else None
307+
if table_kwargs and not table_args:
308+
text += ' __table_args__ = {0}\n'.format(kwargs_items)
309+
elif table_args:
310+
if kwargs_items:
311+
table_args.append(kwargs_items)
297312
if len(table_args) == 1:
298313
table_args[0] += ','
299314
text += ' __table_args__ = (\n {0}\n )\n'.format(',\n '.join(table_args))

test/test_codegen.py

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def remove_unicode_prefixes(text):
2626
class TestCodeGenerator(object):
2727
def setup(self):
2828
self.metadata = MetaData(create_engine('sqlite:///'))
29-
29+
3030
def generate_code(self, **kwargs):
3131
codegen = CodeGenerator(self.metadata, **kwargs)
3232
sio = StringIO()
@@ -793,3 +793,79 @@ class SimpleItems(Base):
793793
794794
id = Column(Integer, primary_key=True)
795795
""")
796+
797+
def test_table_kwargs(self):
798+
Table(
799+
'simple_items', self.metadata,
800+
Column('id', INTEGER, primary_key=True),
801+
schema='testschema'
802+
)
803+
804+
eq_(self.generate_code(), """\
805+
# coding: utf-8
806+
from sqlalchemy import Column, Integer
807+
from sqlalchemy.ext.declarative import declarative_base
808+
809+
810+
Base = declarative_base()
811+
metadata = Base.metadata
812+
813+
814+
class SimpleItem(Base):
815+
__tablename__ = 'simple_items'
816+
__table_args__ = {'schema': 'testschema'}
817+
818+
id = Column(Integer, primary_key=True)
819+
""")
820+
821+
def test_table_args_kwargs(self):
822+
Table(
823+
'simple_items', self.metadata,
824+
Column('id', INTEGER, primary_key=True),
825+
Column('name', VARCHAR),
826+
Index('testidx', 'id', 'name'),
827+
schema='testschema'
828+
)
829+
830+
eq_(self.generate_code(), """\
831+
# coding: utf-8
832+
from sqlalchemy import Column, Index, Integer, String
833+
from sqlalchemy.ext.declarative import declarative_base
834+
835+
836+
Base = declarative_base()
837+
metadata = Base.metadata
838+
839+
840+
class SimpleItem(Base):
841+
__tablename__ = 'simple_items'
842+
__table_args__ = (
843+
Index('testidx', 'id', 'name'),
844+
{'schema': 'testschema'}
845+
)
846+
847+
id = Column(Integer, primary_key=True)
848+
name = Column(String)
849+
""")
850+
851+
def test_schema_table(self):
852+
Table(
853+
'simple_items', self.metadata,
854+
Column('name', VARCHAR),
855+
schema='testschema'
856+
)
857+
858+
eq_(self.generate_code(), """\
859+
# coding: utf-8
860+
from sqlalchemy import Column, MetaData, String, Table
861+
862+
863+
metadata = MetaData()
864+
865+
866+
t_simple_items = Table(
867+
'simple_items', metadata,
868+
Column('name', String),
869+
schema='testschema'
870+
)
871+
""")

0 commit comments

Comments
 (0)