marshmallow-code / marshmallow-sqlalchemy

SQLAlchemy integration with marshmallow
https://marshmallow-sqlalchemy.readthedocs.io
MIT License
549 stars 94 forks source link

How to load all fields into schema from model which loads fields from database directly? #569

Closed fegamon closed 5 months ago

fegamon commented 5 months ago

I have this shema:

class Users(SQLAlchemyAutoSchema):
    class Meta:
        model = models.Users
        include_fk = True
        include_relationships = True

The model is:

class Users(db.Model):
    __table__ = db.Model.metadata.tables['dbo.Users']

As you can see, the columns in the model are directly mapped from db table, and if I print columns from model, all columns are loaded, but if i print the schema, It is just an empty dict. I don't know how to load all fields without set them manually.

sloria commented 5 months ago

looks like you have

    class Meta:
        model: models.Users

rather than

    class Meta:
        model = models.Users

can you try changing that?

fegamon commented 5 months ago

The solution was set fields from model's columns:

class Users(SQLAlchemyAutoSchema):
    class Meta:
        model: models.Users
        include_fk = True
        fields = models.Users.__table__.columns.keys() # This is the solution

I'll keep this issue for people who are facing the same problem.