Sqlacodegen生成混合模型和表 [英] Sqlacodegen generates mixed models and tables
问题描述
执行此命令:
sqlacodegen <connection-url> --outfile db.py
db.py包含生成的表:
The db.py contains generated tables:
t_table1 = Table(...)
和类:
Table2(Base):
__tablename__ = 'table2'
问题在于,表只能以一种方式生成-表或类.
The problem is that a table is generated in one way only - either a table or a class.
我只想让它生成模型(类),但是在提供的标志中我找不到这种选择.有什么主意吗?
I would like to make it generate models (classes) only but in the provided flags I couldn't find such an option. Any idea?
推荐答案
您所描述的似乎是功能本身. sqlacodegen
不会总是生成类模型.
It looks like what you're describing is a feature itself. sqlacodegen
will not always generate class models.
如果表满足所有表的要求,则该表被视为关联表 满足以下条件:
A table is considered an association table if it satisfies all of the following conditions:
- 正好有两个外键约束
- 其所有列均涉及上述约束
尽管如此,您可以尝试快速又肮脏的破解.在源代码中找到这些行(类似于/.../lib/python2.7/site-packages/sqlacodegen/codegen.py
),并注释掉前三行代码(并修复缩进):
Although, you can try a quick and dirty hack. Locate those lines in the source code (something like /.../lib/python2.7/site-packages/sqlacodegen/codegen.py
) and comment out the first three code lines (and fix indentation):
# 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
我已经尝试过针对一个作为表模型生成的特定表进行此操作.它来自
I have tried this for one specific table that was generated as a table model. It went from
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)
您也可以在官方跟踪器中,将其作为功能请求来解决.
You can also open an issue about this as a feature request, in the official tracker.
以防万一,如果您想要相反的设置(仅适用于表模型),则可以使用--noclasses
标志来实现.
Just in case, if you want the opposite (only table models), you could do so with the --noclasses
flag.
这篇关于Sqlacodegen生成混合模型和表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!