From 2c1768bb6171ac26c77b5d5027637a0e6ad625cc Mon Sep 17 00:00:00 2001 From: Abdeldjalil-H Date: Tue, 11 Oct 2022 15:55:08 +0100 Subject: [PATCH] excludee null fields in _construct_relations (#870) * excludee null fields in _construct_relations * add missing test to check empty relation in construct Co-authored-by: collerek --- ormar/models/newbasemodel.py | 1 + tests/test_model_definition/test_model_construct.py | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/ormar/models/newbasemodel.py b/ormar/models/newbasemodel.py index 2780d3c..5ea2098 100644 --- a/ormar/models/newbasemodel.py +++ b/ormar/models/newbasemodel.py @@ -859,6 +859,7 @@ class NewBaseModel(pydantic.BaseModel, ModelTableProxy, metaclass=ModelMetaclass relation_value = [ relation_field.expand_relationship(x, model, to_register=False) for x in value_to_set + if x is not None ] for child in relation_value: diff --git a/tests/test_model_definition/test_model_construct.py b/tests/test_model_definition/test_model_construct.py index 87bd4ba..e2d7876 100644 --- a/tests/test_model_definition/test_model_construct.py +++ b/tests/test_model_definition/test_model_construct.py @@ -60,6 +60,16 @@ def create_test_database(): metadata.drop_all(engine) +@pytest.mark.asyncio +async def test_construct_with_empty_relation(): + async with database: + async with database.transaction(force_rollback=True): + hq = await HQ.objects.create(name="Main") + comp = Company(name="Banzai", hq=None, founded=1988) + comp2 = Company.construct(**dict(name="Banzai", hq=None, founded=1988)) + assert comp.dict() == comp2.dict() + + @pytest.mark.asyncio async def test_init_and_construct_has_same_effect(): async with database: