我遇到过这样的实现,取自here:
from sqlalchemy.sql import expression
from sqlalchemy.ext.compiler import compiles
from sqlalchemy.types import DateTime
class utcnow(expression.FunctionElement):
type = DateTime()
@compiles(utcnow, 'mssql')
def ms_utcnow(element, compiler, **kw):
return "GETUTCDATE()"
我对utcnow()
的使用不在文档中提到的orm声明,但是像这样,似乎是合法的:
session.query(TableA).filter(TableA.created_at >= utcnow()).all()
但添加.all()
或.first()
会产生错误,如:
{AttributeError}Neither 'utcnow' object nor 'Comparator' object has an attribute 'name'
知道为什么吗?