Python 2.7 此表达式不支持sqlachemy混合属性错误-运算符
下面是来自的示例,但无法理解其不起作用的原因Python 2.7 此表达式不支持sqlachemy混合属性错误-运算符,python-2.7,sqlalchemy,Python 2.7,Sqlalchemy,下面是来自的示例,但无法理解其不起作用的原因 class CCStandard(Base): """Codes, grade, code description, and name""" __tablename__ = "DimCCStandard" CCStandardKey = deferred(Column('CCStandardKey', Integer, primary_key=True)) CCStdCode = Column('CCStdCode', String
class CCStandard(Base):
"""Codes, grade, code description, and name"""
__tablename__ = "DimCCStandard"
CCStandardKey = deferred(Column('CCStandardKey', Integer, primary_key=True))
CCStdCode = Column('CCStdCode', String)
#ccstd_info group
CCStdDesc = deferred(Column('CCStdDesc', String), group='ccstd_info')
CCStdName = deferred(Column('CCStdName', String), group='ccstd_info')
CCStdCluster = deferred(Column('CCStdCluster', String), group='ccstd_info')
@hybrid_property
def Cluster(self):
return self.CCStdCode[:1]
下面的简单查询返回“此表达式不支持运算符“getitem”
我肯定这列是一个字符串,所以不知道为什么我会得到这个。如果我尝试使用+运算符,它确实有效,即:
@hybrid_property
def Cluster(self):
return self.CCStdCode + 'well this works'
底层数据库是SQLServer。本专栏讨论的是nvarchar。SQLAlchemy不支持以这种方式进行子字符串查询。如果要查询包含子字符串的列,可以执行以下操作:
session.query(CCStandard).filter(CCStandard.CCStdCode.contains(sub_string))
如果要为子字符串创建自定义表达式,可以按照文档中的说明进行操作。您必须使用表达式
装饰器。下面是一个简单的例子:
class CCStandard(Base):
"""Codes, grade, code description, and name"""
__tablename__ = "ccstandard"
CCStandardKey = Column(Integer, primary_key=True)
_CCStandardCode = Column(String)
@hybrid_property
def CCStandardCode(self):
return self._CCStandardCode
@CCStandardCode.setter
def CCStandardCode(self, code):
self._CCStandardCode = code
@hybrid_property
def Cluster(self):
return self._CCStandardCode[:1]
@Cluster.expression
def Cluster(cls):
# use the SQLite `substr` method to search for a substring
return func.substr(cls._CCStandardCode, 0, 2)
Base.metadata.create_all(engine)
Session = sessionmaker(bind=engine)
session = Session()
>>> c = CCStandard(CCStandardKey=1, CCStandardCode="foo")
>>> session.add(c)
>>> session.commit()
>>> session.query(CCStandard).filter(CCStandard.Cluster == 'f').one()
<foo.CCStandard object at 0x1047305d0>
等级标准(基本):
“”“代码、等级、代码说明和名称”“”
__tablename=“ccstandard”
CCStandardKey=列(整数,主键=True)
_CCStandardCode=列(字符串)
@杂化性
def CCStandardCode(自):
返回自身代码
@CCStandardCode.setter
def CCStandardCode(自身,代码):
self.\CCStandardCode=代码
@杂化性
def集群(自):
返回自我。CCU标准代码[:1]
@Cluster.expression
def集群(cls):
#使用SQLite`substr`方法搜索子字符串
返回函数子字符串(cls.\CCStandardCode,0,2)
Base.metadata.create_all(引擎)
会话=会话生成器(绑定=引擎)
会话=会话()
>>>c=CCStandard(CCStandardKey=1,CCStandardCode=“foo”)
>>>会议.添加(c)
>>>session.commit()
>>>session.query(CCStandard).filter(CCStandard.Cluster=='f').one()
文档中没有您正在做的示例,有一个表达式
装饰器。我将编辑.docs,例如:@hybrid_property def email(self):“返回电子邮件的值,直到最后12个字符。”“返回self.\u email[:-12]
class CCStandard(Base):
"""Codes, grade, code description, and name"""
__tablename__ = "ccstandard"
CCStandardKey = Column(Integer, primary_key=True)
_CCStandardCode = Column(String)
@hybrid_property
def CCStandardCode(self):
return self._CCStandardCode
@CCStandardCode.setter
def CCStandardCode(self, code):
self._CCStandardCode = code
@hybrid_property
def Cluster(self):
return self._CCStandardCode[:1]
@Cluster.expression
def Cluster(cls):
# use the SQLite `substr` method to search for a substring
return func.substr(cls._CCStandardCode, 0, 2)
Base.metadata.create_all(engine)
Session = sessionmaker(bind=engine)
session = Session()
>>> c = CCStandard(CCStandardKey=1, CCStandardCode="foo")
>>> session.add(c)
>>> session.commit()
>>> session.query(CCStandard).filter(CCStandard.Cluster == 'f').one()
<foo.CCStandard object at 0x1047305d0>