![]() ![]() This has been observed with Python 2.7.5 and Flask 0.10.1. People have been bitten by this, as can be seen in these StackOverflow threads: Then db represents the same physical database across threads. All route handlers run within the same thread, so all route handlers interact with the same database. Bootstrap the database from within a route handler.Flask-SQLAlchemy is a Flask extension that makes using SQLAlchemy with Flask easier, providing you tools and methods to interact with your database in your Flask applications through SQLAlchemy. ![]() I guess most often people run into OperationalError: (OperationalError) no such table:, although they think they have already created the corresponding table. It also gives you an Object Relational Mapper (ORM), which allows you to make queries and handle data using Python objects and methods. This is not well-documented and leads to nasty errors. A simple read/query would yield unexpected results.Īgain, in other words: although you push around and interact the same db object in your modules, you might still access different databases, depending on from which thread the interaction takes place. When you then access the database via the same db object (equivalent id()) from within a route handler (which does not run in the main thread), the interaction takes place with a different database, which of course is still empty, the tables are not created. And the mean thing is: the same db object represents different databases, depending on the thread from which it is being used.Įxample: say you call db.create_all() and pre-populate the database in the main thread, before invoking app.run(). ![]() However, in case of an in-memory SQLite database this implementation detail makes a significant difference: the two threads see independent databases. I agree, and usually this detail is not important to be aware of. You might think this should be an implementation detail of how Flask’s development server works under the hood. What difference does it make? A big one, in certain cases: the call happens in different threads (you can easily convince yourself of this by calling threading.current_thread() in the two places, the result is different).
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |