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

many-to-many nullable columns with through #852

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions ormar/fields/many_to_many.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Comment on lines +133 to +134
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you rename it to shorter names: through_relation_nullable and through_reverse_relation_nullable


if through is not None and through.__class__ != ForwardRef:
forbid_through_relations(cast(Type["Model"], through))
Expand Down Expand Up @@ -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), {})
Expand Down
7 changes: 4 additions & 3 deletions ormar/models/helpers/sqlalchemy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add missing tests for setting that column settings nullable/ not nullable and different expected behavior?

)

create_pydantic_field(parent_name, model_field.to, model_field)
Expand All @@ -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.
Expand Down Expand Up @@ -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)
Expand Down
Loading