- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
执行这条命令:
sqlacodegen <connection-url> --outfile db.py
db.py 包含生成的表:
t_table1 = Table(...)
还有类:
Table2(Base):
__tablename__ = 'table2'
问题是表只能以一种方式生成 - 要么是表,要么是类。
我想让它只生成模型(类),但在提供的标志中我找不到这样的选项。有什么想法吗?
最佳答案
看起来您所描述的是一项功能本身。 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据称
A table is considered an association table if it satisfies all of the following conditions:
- has exactly two foreign key constraints
- all its columns are involved in said constraints
不过,您可以尝试一种快速而肮脏的破解方法。在源代码中找到这些行(类似于 /.../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
标志。
关于python - sqlacodegen 生成混合模型和表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34675604/
我在服务器上运行 PostgreSQL 数据库,并尝试使用 SQLAlchemy 连接到它。我发现 sqlacodegen是自动生成元数据对象及其表的好工具。但是当我尝试运行 sqlacodgen p
我正尝试在我的数据库上使用sqlacodegen。 sqlacodegen mysql://root:mypassword@localhost/database_name 但是,我不断收到此错误: s
执行这条命令: sqlacodegen --outfile db.py db.py 包含生成的表: t_table1 = Table(...) 还有类: Table2(Base): __ta
我有一个有用的 PostgreSQL View ,由 JOIN 查询生成: \d very_useful_view; View "public.very_useful_view" Column
当我使用 sqlacodegen 创建 models.py 时,我无法使用 User.query,收到警告“[AttributeError: type object 'User' has no att
我是 Python 和 Flask 生态系统的初学者,正在尝试为研究项目创建一个小型的概念验证 Web 应用程序。我正在使用 Debian Linux 7.9、PostgreSQL 9.5、SQLAl
我在 postgresql 中自动生成表,模型是使用列表中的名称使用 sqlacodegen 自动生成的。 一些名称是非 ascii 字符。对于名称 💞aussieBabe💞,它创建了一个表 💞
我是一名优秀的程序员,十分优秀!