Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #336: detecting relationship as nullable=False #458

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 1 addition & 1 deletion src/marshmallow_sqlalchemy/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,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 @@ -194,7 +194,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 @@ -354,6 +354,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__.attrs.get("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__.attrs.get("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__.attrs.get("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__.attrs.get("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