<p>在更新到SQLAlchemy 2.3之后,我开始遇到这个错误。在</p>
<p>如果您可以删除表参数,它将起作用(但可能会抱怨其他事情,特别是当两个子类共享一个字段名时—这是我使用<code>__table_args__ = {'extend_existing': True}</code>时发生的。在</p>
<p>最后,解决方法是显式地在子类上指定表名,例如:</p>
<pre><code>class Establishment(Model):
__tablename__ = 'establishments'
name = Column(String(32))
class Cafe(Establishment):
__mapper_args__ = {'polymorphic_identity': 'cafe'}
coffee_type = Column(String(32))
class Bar(Establishment):
__tablename__ = 'establishments'
__table_args__ = {'extend_existing': True}
__mapper_args__ = {'polymorphic_identity': 'bar'}
alcohol_license_number = Column(String(40))
bar_type = Column(Enum(BarType)) # defined elsewhere
class Restaurant(Establishment):
__tablename__ = 'establishments'
__table_args__ = {'extend_existing': True}
__mapper_args__ = {'polymorphic_identity': 'bar'}
alcohol_license_number = Column(String(40))
cuisine_list = Column(String(255))
</code></pre>
<p>一个粗略的例子,但希望这个想法是有意义的-餐厅和酒吧都有一个字段来保存他们的酒精许可证信息,所以我们希望这两个使用表中现有的字段,而不是冲突-现在我们必须从SQLAlchemy 2.3开始指定表名。在</p>
<p>我知道这个例子和问题(关于索引)不是完全相同的问题,但是它导致了同样的错误,这个解决方案在这种情况下应该也能起作用。在</p>