Skip to content

Commit e34b037

Browse files
committed
Fixed issue of not being able to remove --flask
1 parent 2194613 commit e34b037

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

sqlacodegen/codegen.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ def _render_column(column, show_name):
151151
column.index = True
152152
kwarg.append('index')
153153
if column.server_default:
154-
server_default = 'server_default=db.FetchedValue()'
154+
server_default = 'server_default=' + _flask_prepend + 'FetchedValue()'
155155

156156
return _flask_prepend + 'Column({0})'.format(', '.join(
157157
([repr(column.name)] if show_name else []) +
@@ -413,7 +413,7 @@ def __init__(self, source_cls, target_cls):
413413
self.backref_name = _underscore(self.source_cls)
414414

415415
def render(self):
416-
text = 'db.relationship('
416+
text = _flask_prepend + 'relationship('
417417
args = [repr(self.target_cls)]
418418

419419
if 'secondaryjoin' in self.kwargs:
@@ -530,6 +530,11 @@ def __init__(self, metadata, noindexes=False, noconstraints=False,
530530

531531
# exclude these column names from consideration when generating association tables
532532
_special_columns = fkcols or []
533+
534+
self.flask = flask
535+
if not self.flask:
536+
global _flask_prepend
537+
_flask_prepend = ''
533538

534539
# Pick association tables from the metadata into their own set, don't process them normally
535540
links = defaultdict(lambda: [])
@@ -612,7 +617,6 @@ def __init__(self, metadata, noindexes=False, noconstraints=False,
612617
relationship.make_backref(visited, classes)
613618
visited.append(relationship)
614619

615-
self.flask = flask
616620
if self.flask:
617621
# Add Flask-SQLAlchemy support
618622
self.collector.add_literal_import('flask_sqlalchemy', 'SQLAlchemy')
@@ -621,6 +625,8 @@ def __init__(self, metadata, noindexes=False, noconstraints=False,
621625
for model in classes.values():
622626
if model.parent_name == 'Base':
623627
model.parent_name = parent_name
628+
else:
629+
self.collector.add_literal_import('sqlalchemy.ext.declarative', 'declarative_base')
624630

625631
def render(self, outfile=sys.stdout):
626632
print(self.header, file=outfile)
@@ -631,7 +637,6 @@ def render(self, outfile=sys.stdout):
631637
if self.flask:
632638
print('db = SQLAlchemy()', file=outfile)
633639
else:
634-
_flask_prepend = ''
635640
if any(isinstance(model, ModelClass) for model in self.models):
636641
print('Base = declarative_base()\nmetadata = Base.metadata', file=outfile)
637642
else:

0 commit comments

Comments
 (0)