Skip to content

Commit

Permalink
Fix #1399 - skip expanding through fields on clone (#1407)
Browse files Browse the repository at this point in the history
* Fix #1399

* run db
  • Loading branch information
collerek authored Dec 4, 2024
1 parent b63b9d3 commit ca55e9c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
6 changes: 5 additions & 1 deletion ormar/models/helpers/relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,11 @@ def expand_reverse_relationships(model: Type["Model"]) -> None:
"""
model_fields = list(model.ormar_config.model_fields.values())
for model_field in model_fields:
if model_field.is_relation and not model_field.has_unresolved_forward_refs():
if (
model_field.is_relation
and not model_field.has_unresolved_forward_refs()
and not model_field.is_through
):
model_field = cast("ForeignKeyField", model_field)
expand_reverse_relationship(model_field=model_field)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import ormar
import pytest

from tests.lifespan import init_tests
from tests.settings import create_config

base_ormar_config = create_config()


class Author(ormar.Model):
ormar_config = base_ormar_config.copy(tablename="authors")

id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=100)


class Book(ormar.Model):
ormar_config = base_ormar_config.copy(tablename="books")

id = ormar.Integer(primary_key=True)
title = ormar.String(max_length=100)
author = ormar.ManyToMany(
Author,
)
year = ormar.Integer(nullable=True)


create_test_database = init_tests(base_ormar_config)


@pytest.mark.asyncio
async def test_tables_are_created():
async with base_ormar_config.database:
assert await Book.objects.all() == []

0 comments on commit ca55e9c

Please sign in to comment.