Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask-SQLAlchemy: SQLALCHEMY_ENGINE_OPTIONS not set up correctly

I just updated my projects Flask-SQLAlchemy Version to the latest one (v2.4). As some of the SQL-Alchemy config parameters were deprecated, I now follow the documentation and added SQLALCHEMY_ENGINE_OPTIONS as a dictionary to my config class. However, when I try to query the database I get an error.

I was looking up the exact keywords needed for sqlalchemy's create_engine().

Here is my config class:

class ConfigAPI:

    try:

        SQLALCHEMY_DATABASE_URI = os.environ['MYSQL_URI']

    except KeyError as e:

        logging.warning('FAILED DEFINING MYSQL PARAMETER')

        logging.fatal(e)

        sys.exit(1)

    SECRET_KEY = '123456asdsadfsdfasadfa67893nvkabl790'
    SQLALCHEMY_ECHO = False
    SQLALCHEMY_TRACK_MODIFICATIONS = False
    SQLALCHEMY_ENGINE_OPTIONS = {
                                'pool': QueuePool,
                                 'pool_size' : 10,
                                 'pool_recycle':120,
                                 'pool_pre_ping': True
                                 }

And here is my app_factory file

db = SQLAlchemy()

def create_api(config=ConfigAPI):
    app = Flask(__name__)

    from app_projects.internal_api.api_v0 import blueprint as v0
    app.config.from_object(config)
    db.init_app(app)
    cors.init_app(app)
    app.register_blueprint(v0)

    return app

This is the error I am receiving:

TypeError: Invalid argument(s) 'pool_size','pool_recycle','pool_pre_ping' sent to create_engine(), using configuration MySQLDialect_pymysql/type/Engine.  Please check that the keyword arguments are appropriate for this combination of components.

What am I missing here?

like image 220
bertharry Avatar asked May 23 '19 08:05

bertharry


1 Answers

As you have already mentioned in the comment the problem was in the pool parameter.

SQLAlchemy documentation specifies that an instance of Pool or QueuePool could be a pool parameter.

The valid example is:

SQLALCHEMY_ENGINE_OPTIONS = {
    'pool': QueuePool(creator),
    'pool_size': 10,
    'pool_recycle': 120,
    'pool_pre_ping': True
}
like image 108
Max Voitko Avatar answered Nov 13 '22 12:11

Max Voitko