-
-
Notifications
You must be signed in to change notification settings - Fork 899
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Model with tablename of reflected table does not have table assigned #550
Comments
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. |
Reopening because it would be good to fix which error gets raised. Will look into this. |
It does have a primary key which gets loaded in during the reflect process. This worked fine before. |
The code you've shown creates a model with no primary key and does not inherit from an existing mapped model. Make sure your example code reflects the problem you're describing. |
No...it does. Look:
This is in v2.2. 2.3 prevents me from loading with the exact same code. |
Do you get any warnings from SQLAlchemy? In your real code, are you doing anything with that model besides setting a tablename? |
I have a feeling this just worked by accident due to other bad behavior in 2.2, and you actually need to inherit from the reflected model and/or use There were never any tests for this, nor the intention for this to work. |
The error is thrown at the time it is loaded with that exact code up there. Here's the full thing.
Besides the little snips for privacy, it's exactly the same code that just worked in 2.2. I don't have time to do anything with the model because I can't load it in. |
I believe I am inheriting properly by loading db from app, then using class X(db.Model). All joins and everything, which would rely on the keys, work properly. |
You are not. |
So I guess I'm not sure why this worked with zero issues before in a fairly complex setup. Are you saying it was a bug of a bug that made it work? If so I feel like that behavior makes sense. What should I be doing instead besides that relatively straightforward-seeming approach? |
Does this work in plain SQLAlchemy or Flask-SQLAlchemy 2.1? |
OK, this does work in plain SQLAlchemy. Probably need to account for it in import sqlalchemy as sa
from sqlalchemy import orm
from sqlalchemy.ext.declarative import declarative_base
engine = sa.create_engine('postgresql:///example', echo=True)
Base = declarative_base()
Base.metadata.reflect(engine)
class User(Base):
__tablename__ = 'user'
session = orm.Session(engine)
print(session.query(User).all()) |
2.1 also works fine. For the sqlalchemy:
|
Oh, you beat me to it. Are you saying I need to account for it or just that it does in F-SA? |
Fixed in #551. Will be released soon, want to look into one other issue first. |
I know this is an old issue, but I've searched high and low trying to find a flask-sqlalchemy reflect example that actually works. Could you point me to a working example (using an app factory)? Much appreciated. |
Just updated and my full application, working previously, broke down on setting the model due to the update (#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:
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.
The text was updated successfully, but these errors were encountered: