Python sqlalchemy编译器问题

Python sqlalchemy编译器问题,python,sqlalchemy,Python,Sqlalchemy,我有一个定制的InsertFromSelect类,它完全按照它的名称执行。输出查询正是我所需要的,问题是我似乎无法执行它 类别: from sqlalchemy.ext.compiler import compiles from sqlalchemy.sql.expression import Executable, ClauseElement class InsertFromSelect( Executable, ClauseElement ): def __init__( self

我有一个定制的InsertFromSelect类,它完全按照它的名称执行。输出查询正是我所需要的,问题是我似乎无法执行它

类别:

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

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

@compiles( InsertFromSelect )
def visit_insert_from_select( element, compiler, **kw ):
    return "INSERT INTO %s (%s)" % (
        compiler.process(element.table, asfrom=True),
        compiler.process(element.select)
    )
查询:

import proxy.lib.sqlalchemy.compilers.insertFromSelect as ifs
from sqlalchemy import not_, literal_column, String, text
from proxy.database import engine

ifsTest = ifs.InsertFromSelect(
    user_ip_table,
    select(
        [
            user.id,
            ips_table.c.id, literal_column( "'inactive'", String ),
            literal_column( "'"+service_type+"'", String )
        ]
    ).where(
        not_( ips_table.c.id.in_(
            select(
                [user_ip_table.c.ip_id]
            )
        ) )
    ).where(
        ips_table.c.ip_address.in_( validIps )
    )
)
查询输出(打印ifsTest):

我已经针对数据库手动测试了查询(当然使用了params),它生成了我所需要的内容,但我似乎无法使用sqlalchemy执行它

I've tried:
connection = engine.connect()
res = connection.execute( ifsTest )
connection.close()

……但没有插入任何内容。知道我该怎么做吗?

既然您没有使用事务,请在您的构造中添加“自动提交”选项:

class InsertFromSelect( Executable, ClauseElement ):
    _execution_options = \
        Executable._execution_options.union({'autocommit': True})
    def __init__( self, table, select ):
        self.table = table
        self.select = select
或者明确地称之为:

connection.execution_options(autocommit=True).execute(mystatement)
或使用交易:

trans = connection.begin()
connection.execute(...)
trans.commit()
背景:


我认为这里没有什么可承诺的。
trans = connection.begin()
connection.execute(...)
trans.commit()