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 showFix it fast
Most common cause, 1 of 3A shared parent model is missing __abstract__ = True
Mark the shared parent as abstract.
class BaseModel(Base):
__abstract__ = True
id: Mapped[int] = mapped_column(primary_key=True)
created_at: Mapped[datetime] = mapped_column(default=func.now())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.
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.
python -c "import app.models"Then find the line you see in this list and open its fix:
- 1A shared parent model is missing __abstract__ = True
The class in the message holds common columns such as id or created_at, and your real models subclass it - 2A mixin class inherits from Base or db.Model
The class in the message is named like a helper (TimestampMixin, SoftDeleteMixin) and its definition line reads class ...Mixin(Base) or (db.Model) - 3A Flask-SQLAlchemy model has no primary key column
The 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.
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.
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.
python -c "from app.models import User; print(User.__table__.columns.keys())"Confirmed by docs.sqlalchemy.org
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.
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.
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.
python -c "from app.models import Invoice; print(Invoice.__table__.columns.keys())"Confirmed by docs.sqlalchemy.org
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.
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.
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.
with app.app_context():
print(AuditEntry.__tablename__)
db.create_all()Confirmed by flask-sqlalchemy.palletsprojects.com
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.