Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Databases and Redis

db.create_all() runs without an error but the tables never appear

Errordb.create_all() finishes with no error and the database stays empty

Short 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.

  • Applies to SQLAlchemy 2.0, Flask-SQLAlchemy 3.1, Python 3.12
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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))
  2. 2Match what you see, then jump to its fix

    1. 1db.metadata.tables is empty, or lacks the model you addedThe model modules were never importedFix
    2. 2RuntimeError about working outside of application contextcreate_all() is called without an application contextFix
    3. 3You changed a column but the database still has the old shapeA table with that name already exists, so it is skippedFix

What does the error look like?

  • db.create_all() finishes with no error and the database stays empty
  • RuntimeError: 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

Did this fix it?

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.py

Confirmed by flask-sqlalchemy.readthedocs.io

Did this fix it?

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

Did this fix it?

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:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.