当with_polymorphic用于带有order_by mapper arg的模型时,SQLAlchemy会在查询时引发异常

时间:2014-03-24 10:58:01

标签: python sqlalchemy

在使用SQLA继承时我遇到了一个问题(我在那里设置了__mapper_args__ mixin。)

重现它:

  • 模型应具有__mapper_arg__属性,并且order_by param设置为任何字符串。
  • 使用此模型添加with_polymorphic('*')调用查询。
class User(Base):
    id = Column(Integer, primary_key=True)
    name = Column(String)

    __mapper_args__ = {'order_by': 'User.name'}

使用这种结构,除非我们将with_polymorphic('*')添加到查询中,否则一切正常。

db.query(User).with_polymorphic('*')

此操作失败,异常

File "/python2.7/dist-packages/sqlalchemy/sql/visitors.py", line 267, in clone
    'no_replacement_traverse' in elem._annotations:
AttributeError: 'str' object has no attribute '_annotations'

我想这是一个错误。但是,由于它在SQLA 0.7-0.9上再现,我对我遇到这个问题的方式表示怀疑。也许我做错了什么?

此问题已转载于small testcase


P.S。

最初我在项目中需要这个mixin:

class DocMixin(object):

    id = Column(Integer)
    title = Column(String(255))

    @declared_attr
    def __mapper_args__(cls):
        return {'order_by': 'title'}

1 个答案:

答案 0 :(得分:0)

不要在order_by mapper参数中使用字符串,直接使用列。将'User.name'更改为name会使您的示例通过测试。