Skip to content

Commit e3f95b9

Browse files
authored
fix index error on double addition of the same related model (#1346)
* fix index error on double addition of the same related model * connect to db * change to cleanup instead of transaction as a check
1 parent ef705cc commit e3f95b9

3 files changed

Lines changed: 101 additions & 1 deletion

File tree

ormar/relations/relation_proxy.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ async def add(self, item: "T", **kwargs: Any) -> None:
325325
:param item: child to add to relation
326326
:type item: Model
327327
"""
328-
new_idx = len(self)
328+
new_idx = len(self) if item not in self else self.index(item)
329329
relation_name = self.related_field_name
330330
await self._owner.signals.pre_relation_add.send(
331331
sender=self._owner.__class__,

tests/test_hashes/__init__.py

Whitespace-only changes.
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import sqlite3
2+
from typing import ForwardRef, List, Optional
3+
4+
import asyncpg
5+
import ormar
6+
import pymysql
7+
import pytest
8+
import pytest_asyncio
9+
10+
from tests.lifespan import init_tests
11+
from tests.settings import create_config
12+
13+
base_ormar_config = create_config(force_rollback=True)
14+
15+
16+
class Category(ormar.Model):
17+
ormar_config = base_ormar_config.copy(tablename="categories")
18+
19+
id: int = ormar.Integer(primary_key=True)
20+
name: str = ormar.String(max_length=40)
21+
22+
23+
class Author(ormar.Model):
24+
ormar_config = base_ormar_config.copy(tablename="authors")
25+
26+
id: int = ormar.Integer(primary_key=True)
27+
name: str = ormar.String(max_length=40)
28+
29+
30+
class Post(ormar.Model):
31+
ormar_config = base_ormar_config.copy(tablename="posts")
32+
33+
id: int = ormar.Integer(primary_key=True)
34+
title: str = ormar.String(max_length=200)
35+
categories: Optional[List[Category]] = ormar.ManyToMany(Category)
36+
authors: Optional[List[Author]] = ormar.ManyToMany(
37+
Author, through=ForwardRef("AuthorXPosts")
38+
)
39+
40+
41+
class AuthorXPosts(ormar.Model):
42+
ormar_config = base_ormar_config.copy(
43+
tablename="authors_x_posts", constraints=[ormar.UniqueColumns("author", "post")]
44+
)
45+
id: int = ormar.Integer(primary_key=True)
46+
author: Optional[int] = ormar.Integer(default=None)
47+
post: Optional[int] = ormar.Integer(default=None)
48+
49+
50+
Post.update_forward_refs()
51+
52+
53+
create_test_database = init_tests(base_ormar_config)
54+
55+
56+
@pytest_asyncio.fixture(scope="function", autouse=True)
57+
async def cleanup():
58+
yield
59+
async with base_ormar_config.database:
60+
await Post.ormar_config.model_fields["categories"].through.objects.delete(
61+
each=True
62+
)
63+
await Post.ormar_config.model_fields["authors"].through.objects.delete(
64+
each=True
65+
)
66+
await Post.objects.delete(each=True)
67+
await Category.objects.delete(each=True)
68+
await Author.objects.delete(each=True)
69+
70+
71+
@pytest.mark.asyncio
72+
async def test_adding_same_m2m_model_twice():
73+
async with base_ormar_config.database:
74+
async with base_ormar_config.database:
75+
post = await Post.objects.create(title="Hello, M2M")
76+
news = await Category(name="News").save()
77+
78+
await post.categories.add(news)
79+
await post.categories.add(news)
80+
81+
categories = await post.categories.all()
82+
assert categories == [news]
83+
84+
85+
@pytest.mark.asyncio
86+
async def test_adding_same_m2m_model_twice_with_unique():
87+
async with base_ormar_config.database:
88+
async with base_ormar_config.database:
89+
post = await Post.objects.create(title="Hello, M2M")
90+
redactor = await Author(name="News").save()
91+
92+
await post.authors.add(redactor)
93+
with pytest.raises(
94+
(
95+
sqlite3.IntegrityError,
96+
pymysql.IntegrityError,
97+
asyncpg.exceptions.UniqueViolationError,
98+
)
99+
):
100+
await post.authors.add(redactor)

0 commit comments

Comments
 (0)