插入中的“ VALUES”是标准SQL,独立的“
VALUES”关键字是Postgresql。PGValues上有一份针对此的快速编译器配方(如果有一天我更改了维基,请在此处复制):
from sqlalchemy import *from sqlalchemy.ext.compiler import compilesfrom sqlalchemy.sql.expression import FromClausefrom sqlalchemy.sql import table, columnclass values(FromClause): def __init__(self, *args): self.list = args def _populate_column_collection(self): self._columns.update( [("column%d" % i, column("column%d" % i)) for i in xrange(1, len(self.list[0]) + 1)] )@compiles(values)def compile_values(element, compiler, asfrom=False, **kw): v = "VALUES %s" % ", ".join( "(%s)" % ", ".join(compiler.render_literal_value(elem, None) for elem in tup) for tup in element.list ) if asfrom: v = "(%s)" % v return vif __name__ == '__main__': t1 = table('t1', column('a'), column('b')) t2 = values((1, 0.5), (2, -0.5)).alias('weights') print select([t1, t2]).select_from(t1.join(t2, t1.c.a==t2.c.column2))


