이름이 말하는 정확히 사용자 지정 InsertFromSelect 클래스가 있습니다. 출력 쿼리가 정확히 내가 원하는, 문제는 그것을 실행할 수없는 것입니다.sqlalchemy 컴파일러 질문
등급 :
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)
)
쿼리 : 나는 PARAMS와 (데이터베이스에 대해 수동으로 쿼리를 테스트 한
INSERT INTO user_ip (SELECT 5144, ip.id, 'inactive', 'proxy'
FROM ip
WHERE ip.id NOT IN (SELECT user_ip.ip_id
FROM user_ip) AND ip.ip_address IN (:ip_address_1, :ip_address_2, :ip_address_3, :ip_address_4, :ip_address_5, :ip_address_6))
:
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) 물론 그 자리에) 그리고 내가 원하는 것을 정확히 생산하지만 나는 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)
또는 트랜잭션 사용하면 트랜잭션을 사용하지 않을 이후
커밋하셨습니까? –
여기에 커밋 할 것이 없다고 생각합니다. http://www.sqlalchemy.org/docs/core/connections.html#dbengine-implicit –