1

I am trying to use flask-admin and flask-sqlalchemy on Google App Engine, but I am receiving the following errors:

pg8000.core.ProgrammingError: {'S': 'ERROR', 'V': 'ERROR', 'C': '42P18', 'M': 'could not determine data type of parameter $1', '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>),))

I am receiving these errors while accessing some flask-admin view (CRUD view). But, when I use non-flask-admin forms and other database manipulations without flask-admin my application is working correctly on Google App Engine.

My setup is listed below

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

My config is defined as follows.

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')

My app.yaml is defined below:

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

Please, how can I solve that issues? All of my application is working, except by flask-admin views. My tables and data is persisted on my database installed on Google Cloud SQL (PostgreSQL 11).

1 Answer 1

3

I solved my issues changing my driver from pg8000 to psycopg2. In my code I just update DATABASE_URI from

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

to

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

psycopg2 uses host param to receive unix sockets and requires only directory path, not file one as pg8000.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.