Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/marshmallow_sqlalchemy/convert.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import enum
import functools
import inspect
import uuid
Expand Down Expand Up @@ -435,16 +436,19 @@ def _add_column_kwargs(self, kwargs: dict[str, Any], column: sa.Column) -> None:
kwargs["validate"].append(validate.OneOf(choices=column.type.enums))

# Add a length validator if a max length is set on the column
# Skip UUID columns
# (see https://github.com/marshmallow-code/marshmallow-sqlalchemy/issues/54)
# Skip UUID and Enum columns
# - https://github.com/marshmallow-code/marshmallow-sqlalchemy/issues/54
# - https://github.com/marshmallow-code/marshmallow-sqlalchemy/issues/673
if hasattr(column.type, "length") and not kwargs.get("dump_only"):
column_length = column.type.length
if column_length is not None:
try:
python_type = column.type.python_type
except (AttributeError, NotImplementedError):
python_type = None
if not python_type or not issubclass(python_type, uuid.UUID):
if not python_type or not issubclass(
python_type, uuid.UUID | enum.Enum
):
kwargs["validate"].append(validate.Length(max=column_length))

if getattr(column.type, "asdecimal", False):
Expand Down
1 change: 1 addition & 0 deletions tests/test_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ def test_enum_with_class_converted_to_enum_field(self, models):
field = fields_["level_with_enum_class"]
assert type(field) is fields.Enum
assert contains_validator(field, validate.OneOf) is False
assert contains_validator(field, validate.Length) is False
assert field.enum is CourseLevel

def test_many_to_many_relationship(self, models):
Expand Down
Loading