fix for not saving related model in reverse rel
This commit is contained in:
@ -99,7 +99,7 @@ course = Course(name="Math", completed=False) # note - not saved
|
|||||||
await department.courses.add(course)
|
await department.courses.add(course)
|
||||||
assert course.pk is not None # child model was saved
|
assert course.pk is not None # child model was saved
|
||||||
# relation on child model is set and FK column saved in db
|
# relation on child model is set and FK column saved in db
|
||||||
assert courses.department == department
|
assert course.department == department
|
||||||
# relation on parent model is also set
|
# relation on parent model is also set
|
||||||
assert department.courses[0] == course
|
assert department.courses[0] == course
|
||||||
```
|
```
|
||||||
@ -112,6 +112,10 @@ assert department.courses[0] == course
|
|||||||
|
|
||||||
That means that in example above the department has to be saved before you can call `department.courses.add()`.
|
That means that in example above the department has to be saved before you can call `department.courses.add()`.
|
||||||
|
|
||||||
|
!!!warning
|
||||||
|
This method will not work on `ManyToMany` relations - there, both sides of the relation have to be saved before adding to relation.
|
||||||
|
|
||||||
|
|
||||||
### remove
|
### remove
|
||||||
|
|
||||||
Removal of the related model one by one.
|
Removal of the related model one by one.
|
||||||
|
|||||||
@ -8,10 +8,13 @@
|
|||||||
## 🐛 Fixes
|
## 🐛 Fixes
|
||||||
|
|
||||||
* Fix is null filter with pagination and relations (by @erichaydel) [#214](https://github.com/collerek/ormar/issues/214)
|
* Fix is null filter with pagination and relations (by @erichaydel) [#214](https://github.com/collerek/ormar/issues/214)
|
||||||
|
* Fix not saving child object on reverse side of the relation if not saved before [#216](https://github.com/collerek/ormar/issues/216)
|
||||||
|
|
||||||
|
|
||||||
## 💬 Other
|
## 💬 Other
|
||||||
|
|
||||||
* Expand [fastapi](https://collerek.github.io/ormar/fastapi) part of the documentation to show samples of using ormar in requests and responses in fastapi.
|
* Expand [fastapi](https://collerek.github.io/ormar/fastapi) part of the documentation to show samples of using ormar in requests and responses in fastapi.
|
||||||
|
* Improve the docs in regard of `default`, `ForeignKey.add` etc.
|
||||||
|
|
||||||
# 0.10.9
|
# 0.10.9
|
||||||
|
|
||||||
|
|||||||
@ -214,7 +214,7 @@ class RelationProxy(Generic[T], list):
|
|||||||
setattr(self._owner, self.field_name, item)
|
setattr(self._owner, self.field_name, item)
|
||||||
else:
|
else:
|
||||||
setattr(item, relation_name, self._owner)
|
setattr(item, relation_name, self._owner)
|
||||||
await item.update()
|
await item.upsert()
|
||||||
await self._owner.signals.post_relation_add.send(
|
await self._owner.signals.post_relation_add.send(
|
||||||
sender=self._owner.__class__,
|
sender=self._owner.__class__,
|
||||||
instance=self._owner,
|
instance=self._owner,
|
||||||
|
|||||||
BIN
tests/test_queries/db.sqlite
Normal file
BIN
tests/test_queries/db.sqlite
Normal file
Binary file not shown.
54
tests/test_queries/test_adding_related.py
Normal file
54
tests/test_queries/test_adding_related.py
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
import databases
|
||||||
|
import pytest
|
||||||
|
import sqlalchemy
|
||||||
|
import asyncio
|
||||||
|
|
||||||
|
import ormar
|
||||||
|
|
||||||
|
from tests.settings import DATABASE_URL
|
||||||
|
|
||||||
|
database = databases.Database(DATABASE_URL)
|
||||||
|
metadata = sqlalchemy.MetaData()
|
||||||
|
|
||||||
|
|
||||||
|
class Department(ormar.Model):
|
||||||
|
class Meta:
|
||||||
|
database = database
|
||||||
|
metadata = metadata
|
||||||
|
|
||||||
|
id: int = ormar.Integer(primary_key=True)
|
||||||
|
name: str = ormar.String(max_length=100)
|
||||||
|
|
||||||
|
|
||||||
|
class Course(ormar.Model):
|
||||||
|
class Meta:
|
||||||
|
database = database
|
||||||
|
metadata = metadata
|
||||||
|
|
||||||
|
id: int = ormar.Integer(primary_key=True)
|
||||||
|
name: str = ormar.String(max_length=100)
|
||||||
|
completed: bool = ormar.Boolean(default=False)
|
||||||
|
department: Optional[Department] = ormar.ForeignKey(Department)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(autouse=True, scope="module")
|
||||||
|
def create_test_database():
|
||||||
|
engine = sqlalchemy.create_engine(DATABASE_URL)
|
||||||
|
metadata.drop_all(engine)
|
||||||
|
metadata.create_all(engine)
|
||||||
|
yield
|
||||||
|
metadata.drop_all(engine)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_adding_relation_to_reverse_saves_the_child():
|
||||||
|
async with database:
|
||||||
|
department = await Department(name="Science").save()
|
||||||
|
course = Course(name="Math", completed=False)
|
||||||
|
|
||||||
|
await department.courses.add(course)
|
||||||
|
assert course.pk is not None
|
||||||
|
assert course.department == department
|
||||||
|
assert department.courses[0] == course
|
||||||
Reference in New Issue
Block a user