db.create_all() runs without an error but the tables never appear
db.create_all() finishes with no error and the database stays emptyShort answer
create_all() only creates tables registered on its MetaData at the moment you call it, and it skips tables that already exist. If nothing appears, the model modules were never imported, the call ran outside an app context, or an old table with the same name blocks a changed model.
Fix it fast
1Find out which cause you have
Print the tables SQLAlchemy knows about right before the create call. If the list is empty, the models were not loaded.
print(list(db.metadata.tables))2Match what you see, then jump to its fix
- 1
db.metadata.tables is empty, or lacks the model you addedThe model modules were never importedFix - 2
RuntimeError about working outside of application contextcreate_all() is called without an application contextFix - 3
You changed a column but the database still has the old shapeA table with that name already exists, so it is skippedFix
- 1
What does the error look like?
db.create_all() finishes with no error and the database stays emptyRuntimeError: Working outside of application context
Cause 1 of 3
The model modules were never imported
1Check
A class only registers its table when the module that defines it is imported. If models live in models.py and nothing imports it before create_all(), the metadata is empty.
print(list(db.metadata.tables))2Fix
Import the models before calling create_all(). In a Flask app, import them in the same file, above the app context block.
from models import User # registers the table
with app.app_context():
db.create_all()3Verify
The table names are listed and exist in the database.
print(list(db.metadata.tables))Confirmed by flask-sqlalchemy.readthedocs.io
Cause 2 of 3
create_all() is called without an application context
1Check
Flask-SQLAlchemy needs an active app context to know which app and database URI to use. A plain script or a shell has none.
grep -n "create_all" -r .2Fix
Wrap the call in app.app_context(), and make sure SQLALCHEMY_DATABASE_URI is set and db.init_app(app) ran first.
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///project.db"
db.init_app(app)
with app.app_context():
db.create_all()3Verify
The script runs once without the RuntimeError.
python create_tables.pyConfirmed by flask-sqlalchemy.readthedocs.io
Cause 3 of 3
A table with that name already exists, so it is skipped
1Check
create_all() first checks for each table and only creates the missing ones. It never alters an existing table, so new columns do not appear.
from sqlalchemy import inspect
print(inspect(db.engine).get_table_names())2Fix
Use a migration tool such as Flask-Migrate (Alembic) for changes to existing tables. In a throw-away development database you can drop the table and run create_all() again.
with app.app_context():
db.drop_all()
db.create_all()3Verify
Inspect the table and confirm the new column is there.
from sqlalchemy import inspect
print([c["name"] for c in inspect(db.engine).get_columns("user")])Confirmed by flask-sqlalchemy.readthedocs.io
How do I stop it coming back?
- Use migrations for every schema change once the app holds real data.
- Import all models in one place (for example a models package) so the metadata is always complete.
- Print the table names in a small startup check in development.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- stegnor.comMy company site: 804 pages from a Python and Jinja generator, with 15 free browser tools.
- MenuloA restaurant discovery platform in Germany, kept current by a Python ingestion pipeline.
- ShopGoAn e-commerce aggregator with a Python scraping engine behind its catalog, past 10K Google Play downloads.
Related fixes
Databases
Installing the Airflow MySQL provider fails on pkg-config or mysqlclient
Building the mysqlclient wheel fails during the install
The Airflow MySQL provider depends on mysqlclient, which pip compiles against the MySQL C client library.
2 causes, each with a fixDatabases
asyncpg InterfaceError: another operation is in progress with SQLAlchemy
InterfaceError raised from a FastAPI endpoint that runs several queries at once
A single database connection can run one operation at a time.
4 causes, each with a fixDatabases
Django cannot load MySQLdb and asks whether mysqlclient is installed
Error loading MySQLdb module: Did you install mysqlclient or MySQL-python?
Django's MySQL backend imports MySQLdb, which is provided by the mysqlclient package.
3 causes, each with a fixDatabases
python manage.py migrate finishes but the tables for my app are missing
relation "myapp_book" does not exist
migrate only applies migration files that exist.
3 causes, each with a fixDatabases
Django save fails on PostgreSQL: null value in a column violates a not-null constraint
django.db.utils.IntegrityError: null value in column "price" of relation "shop_product" violates not-null constraint
A model field is NOT NULL in the database unless it has null=True.
3 causes, each with a fixDatabases
Django migration on PostgreSQL stops with 'pending trigger events'
django.db.utils.OperationalError: cannot ALTER TABLE "mytable" because it has pending trigger events
PostgreSQL runs a Django migration in one transaction.
2 causes, each with a fix