Skip to content

Commit 77fae53

Browse files
author
nikelborm
committed
many-to-many nullable columns with through
1 parent 4f2ba89 commit 77fae53

2 files changed

Lines changed: 8 additions & 3 deletions

File tree

ormar/fields/many_to_many.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ def ManyToMany( # type: ignore
124124

125125
through_relation_name = kwargs.pop("through_relation_name", None)
126126
through_reverse_relation_name = kwargs.pop("through_reverse_relation_name", None)
127+
is_through_relation_column_nullable = kwargs.pop("is_through_relation_column_nullable", None)
128+
is_through_reverse_relation_column_nullable = kwargs.pop("is_through_reverse_relation_column_nullable", None)
127129

128130
if through is not None and through.__class__ != ForwardRef:
129131
forbid_through_relations(cast(Type["Model"], through))
@@ -167,6 +169,8 @@ def ManyToMany( # type: ignore
167169
skip_field=skip_field,
168170
through_relation_name=through_relation_name,
169171
through_reverse_relation_name=through_reverse_relation_name,
172+
is_through_reverse_relation_column_nullable=is_through_reverse_relation_column_nullable,
173+
is_through_relation_column_nullable=is_through_relation_column_nullable,
170174
)
171175

172176
Field = type("ManyToMany", (ManyToManyField, BaseField), {})

ormar/models/helpers/sqlalchemy.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ def adjust_through_many_to_many_model(model_field: "ManyToManyField") -> None:
4444
)
4545

4646
create_and_append_m2m_fk(
47-
model=model_field.to, model_field=model_field, field_name=parent_name
47+
model=model_field.to, model_field=model_field, field_name=parent_name, nullable=model_field.is_through_reverse_relation_column_nullable,
4848
)
4949
create_and_append_m2m_fk(
50-
model=model_field.owner, model_field=model_field, field_name=child_name
50+
model=model_field.owner, model_field=model_field, field_name=child_name, nullable=model_field.is_through_relation_column_nullable,
5151
)
5252

5353
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:
5858

5959

6060
def create_and_append_m2m_fk(
61-
model: Type["Model"], model_field: "ManyToManyField", field_name: str
61+
model: Type["Model"], model_field: "ManyToManyField", field_name: str, nullable: bool
6262
) -> None:
6363
"""
6464
Registers sqlalchemy Column with sqlalchemy.ForeignKey leading to the model.
@@ -88,6 +88,7 @@ def create_and_append_m2m_fk(
8888
name=f"fk_{model_field.through.Meta.tablename}_{model.Meta.tablename}"
8989
f"_{field_name}_{pk_alias}",
9090
),
91+
nullable=nullable,
9192
)
9293
model_field.through.Meta.columns.append(column)
9394
model_field.through.Meta.table.append_column(column)

0 commit comments

Comments
 (0)