fix saving related with pk only model (#1297)
This commit is contained in:
@ -246,6 +246,7 @@ class Model(ModelRow):
|
|||||||
self_fields.pop(self.get_column_name_from_alias(self.ormar_config.pkname))
|
self_fields.pop(self.get_column_name_from_alias(self.ormar_config.pkname))
|
||||||
if _columns:
|
if _columns:
|
||||||
self_fields = {k: v for k, v in self_fields.items() if k in _columns}
|
self_fields = {k: v for k, v in self_fields.items() if k in _columns}
|
||||||
|
if self_fields:
|
||||||
self_fields = self.translate_columns_to_aliases(self_fields)
|
self_fields = self.translate_columns_to_aliases(self_fields)
|
||||||
expr = self.ormar_config.table.update().values(**self_fields)
|
expr = self.ormar_config.table.update().values(**self_fields)
|
||||||
expr = expr.where(self.pk_column == getattr(self, self.ormar_config.pkname))
|
expr = expr.where(self.pk_column == getattr(self, self.ormar_config.pkname))
|
||||||
|
|||||||
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