marshmallow-sqlalchemy icon indicating copy to clipboard operation
marshmallow-sqlalchemy copied to clipboard

Length validation error on Enum fields

Open lafrech opened this issue 6 months ago • 0 comments

This look similar to #54.

class Model(Base):
    __tablename__ = "table"

    field = sqla.Column(sqla.Enum(MyEnum, name="my_enum"))


class ModelSchema(AutoSchema):
    class Meta(AutoSchema.Meta):
        model = Model

    # Code works if this line is commented
    # field = ma.fields.Enum(MyEnum)

I'm getting this error on deserialization:

TypeError: object of type 'MyEnum' has no len()

SQLAlchemy does add a max length to the column and it seems like we're generating a max length validator and running it on the deserialized enum rather than the input string since validators apply after deserialization.

I'd drop the length validator since it is redundant anyway.

My code works if I uncomment the direct field declaration.

I didn't have time to investigate any further but I'm dropping this here for discussion.

lafrech avatar May 20 '25 14:05 lafrech