Declarative SQLAlchemy CREATE SQLITE in memory tables

This is how I set up my database for the application (in Flask):

from sqlalchemy.engine import create_engine from sqlalchemy.orm import scoped_session, create_session from sqlalchemy.ext.declarative import declarative_base engine = None db_session = scoped_session(lambda: create_session(bind=engine, autoflush=False, autocommit=False, expire_on_commit=True)) Base = declarative_base() Base.query = db_session.query_property() def init_engine(uri, **kwargs): global engine engine = create_engine(uri, **kwargs) Base.metadata.create_all(bind=engine) return engine 

If I connect to a file database that already has tables created, everything works fine, but using sqlite: ///: memory: as the target database gives me:

 OperationalError: (OperationalError) no such table: users u'DELETE FROM users' () 

upon request, for example for ex .:

 UsersTable.query.delete() db_session.commit() 

I access this code from unit test. What is the problem?

thanks

Edit:

Application setup:

 app = Flask(__name__) app.config.from_object(__name__) app.secret_key = 'XXX' # presenters from presenters.users import users # register modules (presenters) app.register_module(users) # initialize the database init_engine(db) 
+4
source share
1 answer

The code you entered does not contain a table / class declaration. Are you sure the announcement was made before calling init_engine ()?

+3
source

Source: https://habr.com/ru/post/1337622/


All Articles