我正在尝试在Google App Engine上使用flask-admin和flask-sqlalchemy,但出现以下错误:
pg8000.core.ProgrammingError:{'S':'ERROR','V':'ERROR','C':'42P18','M':'无法确定参数$ 1的数据类型','F ':'postgres.c','L':'1400','R':'exec_parse_message'}
sqlalchemy.exc.ProgrammingError:(pg8000.core.ProgrammingError){'S':'ERROR','V':'ERROR','C':'42P01','M':'关系“用户”不存在”,“ P”:“ 239”,“ F”:“ parse_relation.c”,“ L”:“ 1180”,“ R”:“ parserOpenTable”}
KeyError:('SELECT count(%s)AS count_1 \ nFROM属性',(((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套接字,并且只需要目录路径,而不需要文件1作为pg8000
。