marshmallow-sqlalchemy
marshmallow-sqlalchemy copied to clipboard
Using marshmallow.fields.Enum in TYPE_MAPPING for enum fields
Marshmallow now has fields.Enum from version 3.18. Using this in ModelConverter.SQLA_TYPE_MAPPING for enum fields raises exception.
class ExtendedModelConverter(ModelConverter):
SQLA_TYPE_MAPPING = {
**ModelConverter.SQLA_TYPE_MAPPING,
Enum: fields.Enum,
}
Stacktrace:
File "site-packages/marshmallow/schema.py", line 121, in __new__
klass._declared_fields = mcs.get_declared_fields(
File "site-packages/marshmallow_sqlalchemy/schema.py", line 91, in get_declared_fields
fields.update(mcs.get_declared_sqla_fields(fields, converter, opts, dict_cls))
File "site-packages/marshmallow_sqlalchemy/schema.py", line 130, in get_declared_sqla_fields
converter.fields_for_model(
File "site-packages/marshmallow_sqlalchemy/convert.py", line 154, in fields_for_model
field = base_fields.get(key) or self.property2field(prop)
File "site-packages/marshmallow_sqlalchemy/convert.py", line 198, in property2field
ret = field_class(**field_kwargs)
TypeError: __init__() missing 1 required positional argument: 'enum'
Getting the same error with https://github.com/sqlalchemy/sqlalchemy/releases/tag/rel_2_0_2 it works with 2.0.1. For now I downgraded to sqlalchemy to 2.0.1 but not sure if I need to fix something in code or wait until this repo is fixed.
class UserSchema(SQLAlchemyAutoSchema): File "/usr/local/lib/python3.11/site-packages/marshmallow/schema.py", line 121, in __new__ klass._declared_fields = mcs.get_declared_fields( ^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/marshmallow_sqlalchemy/schema.py", line 91, in get_declared_fields fields.update(mcs.get_declared_sqla_fields(fields, converter, opts, dict_cls)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/marshmallow_sqlalchemy/schema.py", line 130, in get_declared_sqla_fields converter.fields_for_model( File "/usr/local/lib/python3.11/site-packages/marshmallow_sqlalchemy/convert.py", line 154, in fields_for_model field = base_fields.get(key) or self.property2field(prop) ^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/marshmallow_sqlalchemy/convert.py", line 193, in property2field field_class = field_class or self._get_field_class_for_property(prop) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/marshmallow_sqlalchemy/convert.py", line 275, in _get_field_class_for_property column = _base_column(prop.columns[0]) ^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/sqlalchemy/util/langhelpers.py", line 1329, in __getattr__ return self._fallback_getattr(key) ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/sqlalchemy/util/langhelpers.py", line 1298, in _fallback_getattr raise AttributeError(key)
@rimvislt I'm afraid SQLA 2 is not supported at all (CI fails with 2.0). See #488.
And Enum field can't be instantiated like this. It requires an enum as argument.
And Enum field can't be instantiated like this. It requires an enum as argument.
Is there any plan to support marshmallow.fields.Enum? Or there's other ways or work around for this issue?