如何在 Google App Engine 上为 postgresql 设置 flask-admin 和 flask-sqlalchemy?

How to setup flask-admin and flask-sqlalchemy for postgresql on Google App Engine?

我正在尝试在 Google App Engine 上使用 flask-admin 和 flask-sqlalchemy,但我收到以下错误:

pg8000.core.ProgrammingError: {'S': 'ERROR', 'V': 'ERROR', 'C': '42P18', 'M': 'could not determine data type of parameter ', 'F': 'postgres.c', 'L': '1400', 'R': 'exec_parse_message'}

sqlalchemy.exc.ProgrammingError: (pg8000.core.ProgrammingError) {'S': 'ERROR', 'V': 'ERROR', 'C': '42P01', 'M': 'relation "user" does not exist', 'P': '239', 'F': 'parse_relation.c', 'L': '1180', 'R': 'parserOpenTable'}

KeyError: ('SELECT count(%s) AS count_1 \nFROM attribute', ((705, 0, .text_out at 0x7f8a723b8c80>),))

我在访问一些 flask-admin 视图(CRUD 视图)时收到这些错误。但是,当我在没有 flask-admin 的情况下使用非 flask-admin 表单和其他数据库操作时,我的应用程序在 Google App Engine 上正常工作。

下面列出了我的设置

from flask import Flask
from config import Config
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from flask_login import LoginManager
from flask_bootstrap import Bootstrap
from flask_admin import Admin

# APP setup
app = Flask(__name__)
app.config.from_object(Config)

# Databaset setup
db = SQLAlchemy(app)
migrate = Migrate(app, db)

# Bootstrap setup
bootstrap = Bootstrap(app)

# Login setup
login = LoginManager(app)
login.login_view = 'login'

from app import models


# Start Application
@app.before_first_request
def setup():

    db.create_all()

    # Creating default users, group and role
    if not models.User.query.first():

        attributes = {...}

        for k, v in attributes.items():
            attr = models.Attribute(name=k, type=v)
            db.session.add(attr)

        role = models.Role(name='admin')

        group = models.Group(name='Administrator')
        group.roles.append(role)

        user = models.User(username='admin')
        user.set_password('admin')
        user.groups.append(group)

        db.session.add(user)
        db.session.commit()


from app.views import GeneralModelView, UserModelView, RoleModelView, GroupModelView, MyAdminIndexView

# Admin Setup
admin = Admin(app, name='Administration', index_view=MyAdminIndexView())
admin.add_view(RoleModelView(models.Role, db.session))
admin.add_view(GroupModelView(models.Group, db.session))
admin.add_view(UserModelView(models.User, db.session))
admin.add_view(GeneralModelView(models.Model, db.session))
admin.add_view(GeneralModelView(models.ModelVersion, db.session))
admin.add_view(GeneralModelView(models.Attribute, db.session))


# Start Application
from app import routes

我的配置定义如下。

import os

basedir = os.path.abspath(os.path.dirname(__file__))


class Config(object):

    SECRET_KEY = os.environ.get('SECRET_KEY') or 'my-super-key-is-here'

    SQLALCHEMY_TRACK_MODIFICATIONS = False

    SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URI') or \
        'sqlite:///' + os.path.join(basedir, 'app.db')

我的app.yaml定义如下:

runtime: python
env: flex
entrypoint: gunicorn -b :$PORT hub:app

runtime_config:
  python_version: 3

beta_settings:
  cloud_sql_instances: <instance>

env_variables:
  DATABASE_URI: 'postgres+pg8000://<user>:<password>?unix_sock=/cloudsql/<instance>/.s.PGSQL.5432'


manual_scaling:
  instances: 1
resources:
  cpu: 1
  memory_gb: 0.5
  disk_size_gb: 10

请问,我该如何解决这些问题?我的所有应用程序都在工作,除了 flask-admin 视图。我的表格和数据保存在我安装在 Google Cloud SQL (PostgreSQL 11) 上的数据库中。

我解决了将驱动程序从 pg8000 更改为 psycopg2 的问题。在我的代码中,我只是从

更新 DATABASE_URI
postgres+pg8000://<user>:<password>@?unix_sock=/cloudsql/<instance>/.s.PGSQL.5432

postgres+psycopg2://<user>:<password>@/<database>?host=/cloudsql/<instance>

psycopg2 使用 host 参数来接收 unix 套接字并且只需要目录路径,而不是 pg8000.

文件