看起来您所描述的内容本身就是一项功能。 sqlacodegen不会总是生成类模型。
它将仅为具有主键且不是关联表的表形成模型类,如您在source code 中所见:
# 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
此外,在documentation 中指出
如果一个表满足所有
以下条件:
- 正好有两个外键约束
- 它的所有列都包含在所述约束中
不过,您可以尝试快速而肮脏的 hack。在源代码中找到这些行(类似于/.../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)
您也可以在official tracker 中以功能请求的形式提出关于此的问题。
以防万一,如果您想要相反的(仅表模型),您可以使用 --noclasses 标志。