看起来你所描述的本身就是一个功能。sqlacodegen
并不总是生成类模型。
It will 仅为具有主键且不是关联表的表形成模型类,正如您在源代码 https://github.com/agronholm/sqlacodegen/blob/master/sqlacodegen/codegen.py#L354:
# 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
此外,在文档 https://github.com/agronholm/sqlacodegen/据称
如果一个表满足所有的条件,则该表被视为关联表
以下条件:
- 恰好有两个外键约束
- 它的所有列都涉及上述约束
不过,您可以尝试一种快速而肮脏的技巧。在源代码中找到这些行(类似于/.../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)
)
to
class AdminOp(Base):
__tablename__ = 'Admin_op'
id_admin = Column(Integer, nullable=False)
id_op = Column(Integer, nullable=False)
您还可以在功能请求中提出有关此问题的问题官方追踪器 https://bitbucket.org/agronholm/sqlacodegen/issues.
以防万一,如果您想要相反的(仅限表格模型),您可以使用--noclasses
flag.