-
Notifications
You must be signed in to change notification settings - Fork 151
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
Support subclassing models for default attributes with alembic migration support similar to sqlalchemy declarative base. #687
Comments
It's because One workaround: don't use def CRUDModel(table):
class Base:
__tablename__ = table
id = db.Column(db.BigInteger(), primary_key=True)
return Base
class User(db.Model, CRUDModel('users')):
first_name = db.Column(db.Unicode(), default="unnamed")
last_name = db.Column(db.Unicode(), default="unnamed") |
Sweet - that makes sense. I'll test it a bit and write some docs
…On Sun, May 31, 2020, 6:51 AM Tony Wang ***@***.***> wrote:
It's because Base is a model itself (with __tablename__). When User
inherits it, it inherits initialized __table__ so Gino doesn't process it
again.
One workaround: don't use db.Model as parent class of Base, but in User
def CRUDModel(table):
class Base():
__tablename__ = table
id = db.Column(db.BigInteger(), primary_key=True)
return Base
class User(db.Model, CRUDModel('users')):
first_name = db.Column(db.Unicode(), default="unnamed")
last_name = db.Column(db.Unicode(), default="unnamed")
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#687 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AC5CHIOICLNUCKVAH7K6UALRUJHGLANCNFSM4NPAILBA>
.
|
Was just trying to solve this a bit more gracefully and found pydantic's dynamic model generation handy. I haven't played much with sqlalchemy directly, but it should be straightforward to implement recursive checking based on model relations, etc. similar to how tortoiseorm has done.
I forked the repo and submitted this PR to get some initial feedback on what you'd like it to support for more robust pydantic model creation. Assuming it will need things like recursive searching if the column is relational, etc. PR for input: |
NOTE: This might just be because I couldn't find any documentation on the proper way to do this!
Description
I want to be able to subclass GINO db.Model and have the inheritance still respect alembic migration support.
What I Did
I created a subclass factory from db.Model with the intent of adding some common configurable attributes to the model class (For FastAPI
This let me add some default routers programmatically:
The problem is that alembic doesn't recognize the subclass attributes, so when I try and create this subclass:
The id field from the CRUDModel Base is the only attribute that's found by alembic's automigrations generation. Either I'm subclassing this wrong, or it's not supported by the db.Model the same way that sqlalchemy allows for (Subclassing a common base class for default attributes).
The text was updated successfully, but these errors were encountered: