Skip to content
Closed
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
2 changes: 1 addition & 1 deletion ormar/models/model_row.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ def _populate_nested_models_from_row( # noqa: CFQ002
used_prefixes=used_prefixes,
plan_cache=plan_cache,
)
item[model_cls.get_column_name_from_alias(related)] = child
item[related] = child
if (
field.is_multi
and child
Expand Down
74 changes: 74 additions & 0 deletions tests/test_relations/test_relation_name_equals_column_alias.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import pytest

import ormar
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="rna_authors")

id: int = ormar.Integer(primary_key=True)
nick: str = ormar.String(max_length=50, name="writer")


class Post(ormar.Model):
ormar_config = base_ormar_config.copy(tablename="rna_posts")

id: int = ormar.Integer(primary_key=True)
title: str = ormar.String(max_length=50, name="posts", default="t")
writer: Author = ormar.ForeignKey(Author)


class Tag(ormar.Model):
ormar_config = base_ormar_config.copy(tablename="rna_tags")

id: int = ormar.Integer(primary_key=True)
label: str = ormar.String(max_length=50, name="blogs", default="l")


class Blog(ormar.Model):
ormar_config = base_ormar_config.copy(tablename="rna_blogs")

id: int = ormar.Integer(primary_key=True)
blogs: list[Tag] = ormar.ManyToMany(Tag, related_name="tagged")


create_test_database = init_tests(base_ormar_config)


@pytest.mark.asyncio
async def test_select_related_when_target_column_alias_equals_relation_name():
async with base_ormar_config.database:
async with base_ormar_config.database.transaction(force_rollback=True):
author = await Author.objects.create(nick="x")
await Post.objects.create(writer=author)
post = await Post.objects.select_related("writer").get()
assert isinstance(post.writer, Author)
assert post.writer.nick == "x"


@pytest.mark.asyncio
async def test_select_related_reverse_when_target_column_alias_equals_relation_name():
async with base_ormar_config.database:
async with base_ormar_config.database.transaction(force_rollback=True):
author = await Author.objects.create(nick="x")
await Post.objects.create(writer=author)
loaded = await Author.objects.select_related("posts").get()
assert len(loaded.posts) == 1
assert isinstance(loaded.posts[0], Post)
assert loaded.posts[0].title == "t"


@pytest.mark.asyncio
async def test_select_related_m2m_when_target_column_alias_equals_relation_name():
async with base_ormar_config.database:
async with base_ormar_config.database.transaction(force_rollback=True):
blog = await Blog.objects.create()
await blog.blogs.add(await Tag.objects.create())
loaded = await Blog.objects.select_related("blogs").get()
assert len(loaded.blogs) == 1
assert isinstance(loaded.blogs[0], Tag)
assert loaded.blogs[0].label == "l"
Loading