fix saving related with pk only model (#1297)
This commit is contained in:
@ -246,11 +246,12 @@ class Model(ModelRow):
|
||||
self_fields.pop(self.get_column_name_from_alias(self.ormar_config.pkname))
|
||||
if _columns:
|
||||
self_fields = {k: v for k, v in self_fields.items() if k in _columns}
|
||||
self_fields = self.translate_columns_to_aliases(self_fields)
|
||||
expr = self.ormar_config.table.update().values(**self_fields)
|
||||
expr = expr.where(self.pk_column == getattr(self, self.ormar_config.pkname))
|
||||
if self_fields:
|
||||
self_fields = self.translate_columns_to_aliases(self_fields)
|
||||
expr = self.ormar_config.table.update().values(**self_fields)
|
||||
expr = expr.where(self.pk_column == getattr(self, self.ormar_config.pkname))
|
||||
|
||||
await self.ormar_config.database.execute(expr)
|
||||
await self.ormar_config.database.execute(expr)
|
||||
self.set_save_status(True)
|
||||
await self.signals.post_update.send(sender=self.__class__, instance=self)
|
||||
return self
|
||||
|
||||
32
tests/test_model_methods/test_save_related_pk_only.py
Normal file
32
tests/test_model_methods/test_save_related_pk_only.py
Normal file
@ -0,0 +1,32 @@
|
||||
import ormar
|
||||
import pytest
|
||||
|
||||
from tests.lifespan import init_tests
|
||||
from tests.settings import create_config
|
||||
|
||||
base_ormar_config = create_config()
|
||||
|
||||
|
||||
class A(ormar.Model):
|
||||
ormar_config = base_ormar_config.copy()
|
||||
|
||||
id = ormar.Integer(primary_key=True)
|
||||
|
||||
|
||||
class B(ormar.Model):
|
||||
ormar_config = base_ormar_config.copy()
|
||||
|
||||
id = ormar.Integer(primary_key=True)
|
||||
a = ormar.ForeignKey(A)
|
||||
|
||||
|
||||
create_test_database = init_tests(base_ormar_config)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_saving_related_pk_only():
|
||||
async with base_ormar_config.database:
|
||||
async with base_ormar_config.database.transaction(force_rollback=True):
|
||||
a = A()
|
||||
await a.save()
|
||||
await a.save_related(follow=True, save_all=True)
|
||||
Reference in New Issue
Block a user