2013-03-05 29 views

回答

0

以下修改可能會有所幫助:

from sqlalchemy.ext.compiler import compiles 
from sqlalchemy.sql.expression import Executable, ClauseElement 

class InsertFromSelect(Executable, ClauseElement): 
    def __init__(self, table, columns, select): 
     self.table = table 
     self.columns = columns 
     self.select = select 

@compiles(InsertFromSelect) 
def visit_insert_from_select(element, compiler, **kw): 
    return "INSERT INTO %s (%s) %s" % (
     compiler.process(element.table, asfrom=True), 
     ", ".join(element.columns), # @note: not a very safe/robust way to compose SQL 
     compiler.process(element.select) 
    ) 

insert = InsertFromSelect(
     t1, 
     ("col1", "col2", "col3",), 
     select([t2.c.x, t2.c.y, t2.c.z]) 
     ) 
print insert 
相關問題