Postgresql SQLAlchemy核心创建临时表作为
我试图在SQLAlchemy核心中使用PostgreSQLPostgresql SQLAlchemy核心创建临时表作为,postgresql,sqlalchemy,Postgresql,Sqlalchemy,我试图在SQLAlchemy核心中使用PostgreSQL创建临时表foo作为SELECT…查询。我已经看过这些文件了,但是没有找到一个方法 我有一个SQLA语句对象。如何根据结果创建临时表?这就是我的想法。请告诉我这样做是否不对 from sqlalchemy.sql import Select from sqlalchemy.ext.compiler import compiles class CreateTableAs(Select): """Create a CREATE T
创建临时表foo作为SELECT…
查询。我已经看过这些文件了,但是没有找到一个方法
我有一个SQLA语句对象。如何根据结果创建临时表?这就是我的想法。请告诉我这样做是否不对
from sqlalchemy.sql import Select
from sqlalchemy.ext.compiler import compiles
class CreateTableAs(Select):
"""Create a CREATE TABLE AS SELECT ... statement."""
def __init__(self, columns, new_table_name, is_temporary=False,
on_commit_delete_rows=False, on_commit_drop=False, *arg, **kw):
"""By default the table sticks around after the transaction. You can
change this behavior using the `on_commit_delete_rows` or
`on_commit_drop` arguments.
:param on_commit_delete_rows: All rows in the temporary table will be
deleted at the end of each transaction block.
:param on_commit_drop: The temporary table will be dropped at the end
of the transaction block.
"""
super(CreateTableAs, self).__init__(columns, *arg, **kw)
self.is_temporary = is_temporary
self.new_table_name = new_table_name
self.on_commit_delete_rows = on_commit_delete_rows
self.on_commit_drop = on_commit_drop
@compiles(CreateTableAs)
def s_create_table_as(element, compiler, **kw):
"""Compile the statement."""
text = compiler.visit_select(element)
spec = ['CREATE', 'TABLE', element.new_table_name, 'AS SELECT']
if element.is_temporary:
spec.insert(1, 'TEMPORARY')
on_commit = None
if element.on_commit_delete_rows:
on_commit = 'ON COMMIT DELETE ROWS'
elif element.on_commit_drop:
on_commit = 'ON COMMIT DROP'
if on_commit:
spec.insert(len(spec)-1, on_commit)
text = text.replace('SELECT', ' '.join(spec))
return text
我找到一篇文章,它使用一个自定义的Select类来实现这一点:。这个职位已经一年了;这仍然是最惯用的方法吗?显然,我的链接帖子使用的
选择进入
格式并不是PostgreSQL中推荐的方法:(参见“注释”部分)。这与中的可克隆
功能相比如何?