Skip to content

Commit

Permalink
fix #336: detecting relationship as nullable=False
Browse files Browse the repository at this point in the history
  • Loading branch information
indiVar0508 committed Sep 11, 2022
1 parent 4f80ff6 commit ee0d2a5
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/marshmallow_sqlalchemy/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ def _add_relationship_kwargs(self, kwargs, prop):
nullable = True
for pair in prop.local_remote_pairs:
if not pair[0].nullable:
if prop.uselist is True:
if prop.uselist is True or prop.direction.name == "MANYTOONE":
nullable = False
break
kwargs.update({"allow_none": nullable, "required": not nullable})
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ class Lecture(Base):
seminar = relationship(
Seminar, foreign_keys=[seminar_title, seminar_semester], backref="lectures"
)
kw = relationship("Keyword", secondary=lecturekeywords_table)
kw = relationship("Keyword", uselist=False, secondary=lecturekeywords_table)
keywords = association_proxy(
"kw", "keyword", creator=lambda kw: Keyword(keyword=kw)
)
Expand Down
43 changes: 43 additions & 0 deletions tests/test_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,49 @@ class ModelWithArray(Base):
assert field.dump_only is True


class TestRelationShipKwarg:
def test_many_to_one_relationship_kwarg(self, models):
default_converter = ModelConverter()

rel = models.Student.__mapper__.get_property("current_school")
assert rel.direction.name == "MANYTOONE"
assert rel.uselist is False
rel_kwargs = {}
default_converter._add_relationship_kwargs(rel_kwargs, rel)
assert rel_kwargs["allow_none"] is False
assert rel_kwargs["required"] is True

rel = models.School.__mapper__.get_property("students")
assert rel.direction.name == "ONETOMANY"
assert rel.uselist is True
rel_kwargs = {}
default_converter._add_relationship_kwargs(rel_kwargs, rel)
assert rel_kwargs["allow_none"] is False
assert rel_kwargs["required"] is True

def test_many_to_many_with_uselist_relationship_kwarg(self, models):
default_converter = ModelConverter()

rel = models.Student.__mapper__.get_property("courses")
assert rel.direction.name == "MANYTOMANY"
assert rel.uselist is True
rel_kwargs = {}
default_converter._add_relationship_kwargs(rel_kwargs, rel)
assert rel_kwargs["allow_none"] is False
assert rel_kwargs["required"] is True

def test_many_to_many_without_uselist_relationship_kwarg(self, models):
default_converter = ModelConverter()

rel = models.Lecture.__mapper__.get_property("kw")
assert rel.direction.name == "MANYTOMANY"
assert rel.uselist is False
rel_kwargs = {}
default_converter._add_relationship_kwargs(rel_kwargs, rel)
assert rel_kwargs["allow_none"] is True
assert rel_kwargs["required"] is False


def _repr_validator_list(validators):
return sorted(repr(validator) for validator in validators)

Expand Down

0 comments on commit ee0d2a5

Please sign in to comment.