Python中文
首页
教程
问答
标签
搜索
登录
注册
无法在单表继承设置中的childclass上定义yu table_uargs
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>我发现,如果<code>__table_args__</code>出现在单表继承设置中的子类上,SQLAlchemy抛出<code>sqlalchemy.exc.ArgumentError: Can't place __table_args__ on an inherited class with no table</code>。同时,可以在子类上定义一个带有<code>index=True</code>的列,这将以与<code>__table_args__</code>相同的方式修改父表。在</p> <p>以下是我的设置:</p> <pre><code>import sqlalchemy as sa import sqlalchemy.ext.declarative from sqlalchemy.ext.declarative import declarative_base Base = declarative_base() class A(Base): __tablename__ = 'a' id = sa.Column(sa.Integer, primary_key=True) type = sa.Column(sa.Text, nullable=False) __mapper_args__ = { 'polymorphic_on': type, } class B(A): b = sa.Column(sa.Integer, index=True) __mapper_args__ = { 'polymorphic_identity': 'b', } class C(A): c = sa.Column(sa.Integer) __mapper_args__ = { 'polymorphic_identity': 'c', } __table_args__ = ( sa.Index('ix_test', c), ) engine = sa.engine.create_engine("sqlite://", echo=True) Base.metadata.create_all(engine) session = sa.orm.Session(engine) session.add_all([ B() ]) session.commit() print(session.query(A)) </code></pre> <p>它抛出:</p> ^{pr2}$ <p>有人知道什么变通办法吗?在</p>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>索引可以放在表定义之外。在</p> <pre><code>class C(A): c = sa.Column(sa.Integer) __mapper_args__ = { 'polymorphic_identity': 'c', } Index('ix_test', C.c) </code></pre>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
如何修复设置.py尝试构建Visual Studio代码python解决方案时出错
3 回答
如何修复设置为检查哪些数字是素数的代码?
3 回答
如何修复设置的环境变量GDAL_DATA路径?
9 回答
如何修复访问“DJANGO_DEBUG”变量时的KeyError
2 回答
如何修复访问API时的“无效身份验证凭据”错误
4 回答
如何修复识别器。听着()在python中用于语音识别
3 回答
如何修复试图读取excel文件时被拒绝的[Errno13]权限?
1 回答
如何修复该函数,使生成的数据帧只包含子集列?
2 回答
如何修复语句弃用警告
1 回答
如何修复语法错误:不能在elemen上使用绝对路径
3 回答
如何修复语法错误:不能在Python上赋值给literal
2 回答
如何修复语法错误:无效语法?
1 回答
如何修复语法错误:无法分配给li
1 回答
如何修复语音识别器python3中的“权限错误:[Errno 13]”
9 回答
如何修复语音频道连接上的添加角色
8 回答
如何修复请求(urllib3)与app engine一起工作?
10 回答
如何修复请求中的“TypeError:'dict'object is not callable”(TypeError:'dict'对象不可调用)
1 回答
如何修复请求中的“启用javascript”错误?
5 回答
如何修复读取csv fi时的非法换行值
3 回答
如何修复读取JSON-fi时“list”对象没有属性“values”
5 回答