flask-sqlalchemy: Model with tablename of reflected table does not have table assigned
Just updated and my full application, working previously, broke down on setting the model due to the update (https://github.com/mitsuhiko/flask-sqlalchemy/pull/541). I believe it might be a problem with this new primary key detection thing, which I’m not sure I get the point of. The previously-fine code is thus:
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
db = SQLAlchemy(app)
db.reflect()
class Country(db.Model):
__tablename__ = 'd_country'
Resulting in:
InvalidRequestError: Class <class 'app.models.Country'> does not have a __table__ or __tablename__ specified and does not inherit from an existing table-mapped class.
I’d be willing to bet that the update didn’t take db.reflect() into account. I’m rolling back unless there’s something else I can do, but I’d rather not have to do a declared_attr on all my tables just for this thing.
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 1
- Comments: 17 (9 by maintainers)
You need to specify a primary key column for all models that do not inherit from another mapped model. This is true in normal SQLAlchemy too, the error messages are just switched due to some internal behavior.