我想将结果返回到自定义模型(类)但我收到此错误消息:
预期的SQL表达式,列或映射实体
我不知道我哪里出错了。
connect = Connector()
Session = sessionmaker(bind=connect.ConnectorMySql())
ses = Session()
query = u"""
SELECT
`reports`.ID As 'ID',
reports.Title AS 'ReportTitle',
`reports`.Text as 'ReporText',
`reports`.Status as 'Status',
`user`.ID AS 'ReporterID',
`user`.Name as 'ReporterName' ,
`user`.Username as 'ReporterUserName',
`user`.ImageProfile as 'ReporterAvatar',
`Clinet`.ID AS 'ClinetID',
`Clinet`.SiteUserName AS 'ClinetUserName',
`Clinet`.ImageProfile as 'ClinetAvatar'
FROM reports
JOIN Clinet on reports.ClinetID = `Clinet`.ID
JOIN users user on reports.UserID = `user`.ID
where
:pClinetID IS NULL OR reports.ClinetID=:pClinetID
AND
:pStatus IS NULL OR reports.Status=:pStatus;
"""
QueryResult=CustomModel()
QueryResult=ses.query(CustomModel).from_statement(query).all()
return QueryResult
答案 0 :(得分:0)
要在SQLAlchemy中使用查询字符串,您必须使用text function制作它们。
所以你应该使用
QueryResult=ses.query(CustomModel).from_statement(text(query)).all()