执行此命令:
sqlacodegen <connection-url> --outfile db.py db.py包含生成的表:
t_table1 = Table(...)课程也包括:
Table2(Base):
__tablename__ = 'table2'问题是,表只以一种方式生成--表或类。
我只想让它生成模型(类),但是在提供的标志中,我找不到这样的选项。有什么想法吗?
发布于 2016-03-10 12:48:30
看起来你所描述的是一个特性本身。sqlacodegen并不总是生成类模型。
它将只为具有主键且不是关联表的表形成模型类,正如您在源代码中所看到的那样。
# Only form model classes for tables that have a primary key and are not association tables
if noclasses or not table.primary_key or table.name in association_tables:
model = self.table_model(table)
else:
model = self.class_model(table, links[table.name], self.inflect_engine, not nojoined)
classes[model.name] = model此外,在文档中指出
如果一个表满足以下所有条件,则该表被视为关联表:
虽然,你可以尝试一个快速和肮脏的黑客。在源代码中找到这些行(类似于/.../lib/python2.7/site-packages/sqlacodegen/codegen.py),并注释掉前三行代码(以及修复缩进):
# Only form model classes for tables that have a primary key and are not association tables
# if noclasses or not table.primary_key or table.name in association_tables:
# model = self.table_model(table)
# else:
model = self.class_model(table, links[table.name], self.inflect_engine, not nojoined)
classes[model.name] = model我尝试过这样做,因为一个特定的表是作为表模型生成的。它从
t_Admin_op = Table(
'Admin_op', metadata,
Column('id_admin', Integer, nullable=False),
Column('id_op', Integer, nullable=False)
)至
class AdminOp(Base):
__tablename__ = 'Admin_op'
id_admin = Column(Integer, nullable=False)
id_op = Column(Integer, nullable=False)您还可以在官方跟踪器中作为特性请求打开有关问题。
以防万一,如果您想要的是相反的(只有表模型),您可以使用--noclasses标志这样做。
https://stackoverflow.com/questions/34675604
复制相似问题