Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Databases and Redis

A shared SQLAlchemy base model or mixin breaks inheritance with a missing-table error

Your error

sqlalchemy.exc.InvalidRequestError: Class <class 'app.models.BaseModel'> does not have a __table__ or __tablename__ specified and does not inherit from an existing table-mapped class.1 more line it can show

Fix it fast

Most common cause, 1 of 3

A shared parent model is missing __abstract__ = True

Mark the shared parent as abstract.

Fix
class BaseModel(Base):
    __abstract__ = True
    id: Mapped[int] = mapped_column(primary_key=True)
    created_at: Mapped[datetime] = mapped_column(default=func.now())
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

SQLAlchemy raises InvalidRequestError "Class ... does not have a __table__ or __tablename__ specified and does not inherit from an existing table-mapped class" when a subclass of Base gets mapped with no table of its own. Usual causes: a shared base model lacking __abstract__ = True, a mixin built on Base, or a Flask-SQLAlchemy model with no primary key. Applies to SQLAlchemy 2.1.

  • Applies to SQLAlchemy 2.1.9, Flask-SQLAlchemy 3.1, Python 3.11 to 3.15
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Import the models module on its own and read which class the message names. Then look at how that class is declared: a parent for other models, a mixin, or a model of its own.

Run this first
python -c "import app.models"

Then find the line you see in this list and open its fix:

  1. 1A shared parent model is missing __abstract__ = TrueThe class in the message holds common columns such as id or created_at, and your real models subclass it
  2. 2A mixin class inherits from Base or db.ModelThe class in the message is named like a helper (TimestampMixin, SoftDeleteMixin) and its definition line reads class ...Mixin(Base) or (db.Model)
  3. 3A Flask-SQLAlchemy model has no primary key columnThe project uses db.Model, the class has no __tablename__, and none of its columns sets primary_key=True

What does the error look like?

  • sqlalchemy.exc.InvalidRequestError: Class <class 'app.models.BaseModel'> does not have a __table__ or __tablename__ specified and does not inherit from an existing table-mapped class.
  • The error is raised while the models module is imported, before any query runs

Cause 1 of 3

A shared parent model is missing __abstract__ = True

You seeThe class in the message holds common columns such as id or created_at, and your real models subclass it

Check

This InvalidRequestError appears when a class written only to share columns subclasses Base, because declarative then tries to map that parent as a table too. It has no __tablename__, and nothing above it is mapped, so SQLAlchemy stops. Setting __abstract__ tells declarative to skip the table and mapper for that class while subclasses still pick up its columns.

Check
class BaseModel(Base):            # no __tablename__, no __abstract__
    id: Mapped[int] = mapped_column(primary_key=True)
    created_at: Mapped[datetime] = mapped_column(default=func.now())

class User(BaseModel):
    __tablename__ = "users"
    email: Mapped[str]

Fix

Mark the shared parent as abstract. Each concrete model keeps its own __tablename__ and gets the inherited columns in its own table.

Fix
class BaseModel(Base):
    __abstract__ = True
    id: Mapped[int] = mapped_column(primary_key=True)
    created_at: Mapped[datetime] = mapped_column(default=func.now())

Verify

The import succeeds and the users table lists the inherited columns.

Verify
python -c "from app.models import User; print(User.__table__.columns.keys())"

Confirmed by docs.sqlalchemy.org

Did this fix it?

Cause 2 of 3

A mixin class inherits from Base or db.Model

You seeThe class in the message is named like a helper (TimestampMixin, SoftDeleteMixin) and its definition line reads class ...Mixin(Base) or (db.Model)

Check

A mixin that subclasses Base is a mapped class in SQLAlchemy's eyes, not a mixin, so it raises the same missing-table error. In the declarative mixin pattern the mixin is a plain Python class, and each model lists it next to Base.

Check
class TimestampMixin(Base):       # wrong: makes the mixin a model
    updated_at: Mapped[datetime]

Fix

Drop Base from the mixin's bases and put the mixin first in each model's base list.

Fix
class TimestampMixin:
    updated_at: Mapped[datetime]

class Invoice(TimestampMixin, Base):
    __tablename__ = "invoices"
    id: Mapped[int] = mapped_column(primary_key=True)

Verify

The mixin column shows up on the model's table, and no table exists for the mixin.

Verify
python -c "from app.models import Invoice; print(Invoice.__table__.columns.keys())"

Confirmed by docs.sqlalchemy.org

Did this fix it?

Cause 3 of 3

A Flask-SQLAlchemy model has no primary key column

You seeThe project uses db.Model, the class has no __tablename__, and none of its columns sets primary_key=True

Check

Flask-SQLAlchemy fills in __tablename__ for you only when the model defines a primary key. Leave the key out and no name is generated, so plain SQLAlchemy reports that the class has no table.

Check
class AuditEntry(db.Model):
    action: Mapped[str]
    created_at: Mapped[datetime]

Fix

Add a primary key column, which a mapped table needs anyway. The table name is then generated from the class name; set __tablename__ yourself if you want a specific one.

Fix
class AuditEntry(db.Model):
    id: Mapped[int] = mapped_column(primary_key=True)
    action: Mapped[str]
    created_at: Mapped[datetime]

Verify

Inside an app context the model reports a table name, and db.create_all() creates it.

Verify
with app.app_context():
    print(AuditEntry.__tablename__)
    db.create_all()

Confirmed by flask-sqlalchemy.palletsprojects.com

Did this fix it?

How do I stop it coming back?

  • Keep shared column sets in plain mixin classes, and reserve classes that subclass Base for real tables or ones marked __abstract__ = True.
  • Add a test that imports every models module and calls Base.metadata.create_all() against an in-memory SQLite database, so a broken class fails CI instead of the first deploy.
  • Give every model an explicit __tablename__; renaming a class then cannot rename its table by accident.

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 on the same stack: stegnor.com, Menulo, ShopGo.

Next step

Want this handled for good?

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