3
votes

In my Config class, I want to set the check_same_thread to False as below (to avoid the error: sqlalchemy.exc.ProgrammingError: (sqlite3.ProgrammingError) SQLite objects created in a thread can only be used in that same thread. .....)

class Config(object):  
    #------#
    SQLALCHEMY_DATABASE_URI = 'sqlite:///app.db?check_same_thread=False'
    #------#

However, in setting up my Config class of the Flask app, I followed Miguel Grinberg's Flask Mega-tutorial and set up the SQLAlchemy database as follows:

class Config(object): 
    #------#
    SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL') or \
        'sqlite:///'+os.path.join(basedir, 'app.db')
    SQLALCHEMY_TRACK_MODIFICATIONS = False
   #------#

How do I integrate the set the check_same_thread to False in the above case? Any help or guidance would be highly appreciated.

Thanks!

2

2 Answers

5
votes

Actually the answer that worked for my question was:

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

Note that the '?check_same_thread=False' part should be added to both cases on either side of the or.

0
votes

You can try something like this:

class Config(object): 
    #------#
    SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL') or \
        'sqlite:///'+os.path.join(basedir, 'app.db') + '?check_same_thread=False'
    SQLALCHEMY_TRACK_MODIFICATIONS = False
   #------#