问题:SQLAlchemy 在 PostgresQL 中创建视图

我正在尝试使用 SQLAlchemy 创建一个视图,并将 Postgresql 作为基础数据库。用于创建视图的单独选择查询运行良好并返回结果,但是当我在创建视图中使用它时,我收到错误 sqlalchemy.exc.NoSuchTableError: Popular,这意味着未选择视图。当我尝试从视图中选择时出现错误。创建视图不会引发任何错误,但不会创建视图。这是我的代码:

from sqlalchemy import *
import sqlalchemy as db
from sqlalchemy import func
from sqlalchemy import desc
from sqlalchemy import Table
from sqlalchemy.ext.compiler import compiles
from sqlalchemy.sql.expression import Executable, ClauseElement

try:
    engine = db.create_engine('postgresql://user:pass@localhost:5432/db_name')
    connection = engine.connect()

except:
    print('Error establishing DB connection')

# Import metadata
metadata = db.MetaData()

# Import articles, authors and log tables
art = db.Table('articles', metadata, autoload=True, autoload_with=engine)
aut = db.Table('authors', metadata, autoload=True, autoload_with=engine)
log = db.Table('log', metadata, autoload=True, autoload_with=engine)


class CreateView(Executable, ClauseElement):
    def __init__(self, name, select):
        self.name = name
        self.select = select


@compiles(CreateView)
def visit_create_view(element, compiler, **kw):
    return "CREATE VIEW %s AS %s" % (
         element.name,
         compiler.process(element.select, literal_binds=True)
         )



# Method to create view with top three articles
def view_top_three():
    top_three_view = CreateView('popular', db.select([art.columns.title, func.count(log.columns.path)]) \
        .where(func.concat('/article/', art.columns.slug) == log.columns.path) \
        .where(log.columns.path != "/") \
        .group_by(log.columns.path, art.columns.title) \
        .order_by(desc(func.count(log.columns.path))) \
        .limit(3))

    engine.execute(top_three_view)
    v = Table('popular', metadata, autoload=True, autoload_with=engine)
    for r in engine.execute(v.select()):
        print(r)


# Call the method which creates view and selects from view
view_top_three()

任何帮助将不胜感激。

解答

我找到了解决方案。问题与自动提交有关。在创建引擎时将 autocommit 设置为 true 解决了如下问题:

engine = db.create_engine('postgresql://user:pass@localhost:5432/db_name').execution_options(autocommit=True)

特别提及@ilja-everilä

Logo

PostgreSQL社区为您提供最前沿的新闻资讯和知识内容

更多推荐