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: