Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask-Sqlalchemy setup engine configuration

SqlAlchemy extension: https://pythonhosted.org/Flask-SQLAlchemy/index.html

and i want to setup the engine with customer configuration using the parameters here: http://docs.sqlalchemy.org/en/rel_0_9/core/engines.html

I'm using the way described on Flask-SqlAlchemy documentation:

from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db'
db = SQLAlchemy(app)


class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(80), unique=True)
    email = db.Column(db.String(120), unique=True)

    def __init__(self, username, email):
        self.username = username
        self.email = email

    def __repr__(self):
        return '<User %r>' % self.username

EDIT: How can I pass the following engine parameters in this kind of configuration:

isolation_level = 'AUTOCOMMIT', encoding='latin1', echo=True

method: SQLAlchemy() doesn't take these as arguments.

like image 920
Ankit Avatar asked Oct 12 '15 19:10

Ankit


1 Answers

it's an open issue: https://github.com/mitsuhiko/flask-sqlalchemy/issues/166

you can try this

class SQLiteAlchemy(SQLAlchemy):
    def apply_driver_hacks(self, app, info, options):
        options.update({
            'isolation_level': 'AUTOCOMMIT', 
            'encoding': 'latin1', 
            'echo': True
        })
        super(SQLiteAlchemy, self).apply_driver_hacks(app, info, options)

db = SQLiteAlchemy(app)
like image 90
r-m-n Avatar answered Sep 29 '22 07:09

r-m-n