diff --git a/ormar/fields/many_to_many.py b/ormar/fields/many_to_many.py index 329d33493..aba93e8ce 100644 --- a/ormar/fields/many_to_many.py +++ b/ormar/fields/many_to_many.py @@ -130,6 +130,8 @@ def ManyToMany( # type: ignore through_relation_name = kwargs.pop("through_relation_name", None) through_reverse_relation_name = kwargs.pop("through_reverse_relation_name", None) + is_through_relation_column_nullable = kwargs.pop("is_through_relation_column_nullable", None) + is_through_reverse_relation_column_nullable = kwargs.pop("is_through_reverse_relation_column_nullable", None) if through is not None and through.__class__ != ForwardRef: forbid_through_relations(cast(Type["Model"], through)) @@ -173,6 +175,8 @@ def ManyToMany( # type: ignore skip_field=skip_field, through_relation_name=through_relation_name, through_reverse_relation_name=through_reverse_relation_name, + is_through_reverse_relation_column_nullable=is_through_reverse_relation_column_nullable, + is_through_relation_column_nullable=is_through_relation_column_nullable, ) Field = type("ManyToMany", (ManyToManyField, BaseField), {}) diff --git a/ormar/models/helpers/sqlalchemy.py b/ormar/models/helpers/sqlalchemy.py index 1a4fd974d..c04a0f393 100644 --- a/ormar/models/helpers/sqlalchemy.py +++ b/ormar/models/helpers/sqlalchemy.py @@ -44,10 +44,10 @@ def adjust_through_many_to_many_model(model_field: "ManyToManyField") -> None: ) create_and_append_m2m_fk( - model=model_field.to, model_field=model_field, field_name=parent_name + model=model_field.to, model_field=model_field, field_name=parent_name, nullable=model_field.is_through_reverse_relation_column_nullable, ) create_and_append_m2m_fk( - model=model_field.owner, model_field=model_field, field_name=child_name + model=model_field.owner, model_field=model_field, field_name=child_name, nullable=model_field.is_through_relation_column_nullable, ) create_pydantic_field(parent_name, model_field.to, model_field) @@ -58,7 +58,7 @@ def adjust_through_many_to_many_model(model_field: "ManyToManyField") -> None: def create_and_append_m2m_fk( - model: Type["Model"], model_field: "ManyToManyField", field_name: str + model: Type["Model"], model_field: "ManyToManyField", field_name: str, nullable: bool ) -> None: """ Registers sqlalchemy Column with sqlalchemy.ForeignKey leading to the model. @@ -91,6 +91,7 @@ def create_and_append_m2m_fk( name=f"fk_{model_field.through.ormar_config.tablename}_{model.ormar_config.tablename}" f"_{field_name}_{pk_alias}", ), + nullable=nullable, ) model_field.through.ormar_config.columns.append(column) model_field.through.ormar_config.table.append_column(column)