fix issue #68
This commit is contained in:
@ -1,3 +1,7 @@
|
|||||||
|
# 0.7.1
|
||||||
|
|
||||||
|
* Fix for overwriting related models with pk only in `Model.save()` (fix [#68][#68])
|
||||||
|
|
||||||
# 0.7.0
|
# 0.7.0
|
||||||
|
|
||||||
* **Breaking:** QuerySet `bulk_update` method now raises `ModelPersistenceError` for unsaved models passed instead of `QueryDefinitionError`
|
* **Breaking:** QuerySet `bulk_update` method now raises `ModelPersistenceError` for unsaved models passed instead of `QueryDefinitionError`
|
||||||
@ -179,3 +183,4 @@ Add queryset level methods
|
|||||||
|
|
||||||
[#19]: https://github.com/collerek/ormar/issues/19
|
[#19]: https://github.com/collerek/ormar/issues/19
|
||||||
[#60]: https://github.com/collerek/ormar/issues/60
|
[#60]: https://github.com/collerek/ormar/issues/60
|
||||||
|
[#68]: https://github.com/collerek/ormar/issues/68
|
||||||
@ -44,7 +44,7 @@ class UndefinedType: # pragma no cover
|
|||||||
|
|
||||||
Undefined = UndefinedType()
|
Undefined = UndefinedType()
|
||||||
|
|
||||||
__version__ = "0.7.0"
|
__version__ = "0.7.1"
|
||||||
__all__ = [
|
__all__ = [
|
||||||
"Integer",
|
"Integer",
|
||||||
"BigInteger",
|
"BigInteger",
|
||||||
|
|||||||
@ -195,7 +195,13 @@ class Model(NewBaseModel):
|
|||||||
if not self.pk and self.Meta.model_fields[self.Meta.pkname].autoincrement:
|
if not self.pk and self.Meta.model_fields[self.Meta.pkname].autoincrement:
|
||||||
self_fields.pop(self.Meta.pkname, None)
|
self_fields.pop(self.Meta.pkname, None)
|
||||||
self_fields = self.populate_default_values(self_fields)
|
self_fields = self.populate_default_values(self_fields)
|
||||||
self.from_dict(self_fields)
|
self.from_dict(
|
||||||
|
{
|
||||||
|
k: v
|
||||||
|
for k, v in self_fields.items()
|
||||||
|
if k not in self.extract_related_names()
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
await self.signals.pre_save.send(sender=self.__class__, instance=self)
|
await self.signals.pre_save.send(sender=self.__class__, instance=self)
|
||||||
|
|
||||||
|
|||||||
53
tests/test_saving_related.py
Normal file
53
tests/test_saving_related.py
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
from typing import Union
|
||||||
|
|
||||||
|
import databases
|
||||||
|
import ormar
|
||||||
|
import pytest
|
||||||
|
import sqlalchemy as sa
|
||||||
|
|
||||||
|
from tests.settings import DATABASE_URL
|
||||||
|
|
||||||
|
engine = sa.create_engine(DATABASE_URL, connect_args={"check_same_thread": False})
|
||||||
|
metadata = sa.MetaData()
|
||||||
|
db = databases.Database(DATABASE_URL)
|
||||||
|
|
||||||
|
|
||||||
|
class Category(ormar.Model):
|
||||||
|
class Meta:
|
||||||
|
tablename = "categories"
|
||||||
|
metadata = metadata
|
||||||
|
database = db
|
||||||
|
|
||||||
|
id: int = ormar.Integer(primary_key=True)
|
||||||
|
name: str = ormar.String(max_length=50, unique=True, index=True)
|
||||||
|
code: int = ormar.Integer()
|
||||||
|
|
||||||
|
|
||||||
|
class Workshop(ormar.Model):
|
||||||
|
class Meta:
|
||||||
|
tablename = "workshops"
|
||||||
|
metadata = metadata
|
||||||
|
database = db
|
||||||
|
|
||||||
|
id: int = ormar.Integer(primary_key=True)
|
||||||
|
topic: str = ormar.String(max_length=255, index=True)
|
||||||
|
category: Union[ormar.Model, Category] = ormar.ForeignKey(
|
||||||
|
Category, related_name="workshops", nullable=False
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def create_test_database():
|
||||||
|
metadata.create_all(engine)
|
||||||
|
yield
|
||||||
|
metadata.drop_all(engine)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_model_relationship(create_test_database):
|
||||||
|
cat = await Category(name="Foo", code=123).save()
|
||||||
|
ws = await Workshop(topic="Topic 1", category=cat).save()
|
||||||
|
|
||||||
|
assert ws.id == 1
|
||||||
|
assert ws.topic == "Topic 1"
|
||||||
|
assert ws.category.name == "Foo"
|
||||||
@ -78,6 +78,7 @@ def test_passing_not_callable():
|
|||||||
|
|
||||||
def test_passing_callable_without_kwargs():
|
def test_passing_callable_without_kwargs():
|
||||||
with pytest.raises(SignalDefinitionError):
|
with pytest.raises(SignalDefinitionError):
|
||||||
|
|
||||||
@pre_save(Album)
|
@pre_save(Album)
|
||||||
def trigger(sender, instance): # pragma: no cover
|
def trigger(sender, instance): # pragma: no cover
|
||||||
pass
|
pass
|
||||||
@ -87,6 +88,7 @@ def test_passing_callable_without_kwargs():
|
|||||||
async def test_signal_functions(cleanup):
|
async def test_signal_functions(cleanup):
|
||||||
async with database:
|
async with database:
|
||||||
async with database.transaction(force_rollback=True):
|
async with database.transaction(force_rollback=True):
|
||||||
|
|
||||||
@pre_save(Album)
|
@pre_save(Album)
|
||||||
async def before_save(sender, instance, **kwargs):
|
async def before_save(sender, instance, **kwargs):
|
||||||
await AuditLog(
|
await AuditLog(
|
||||||
@ -186,6 +188,7 @@ async def test_signal_functions(cleanup):
|
|||||||
async def test_multiple_signals(cleanup):
|
async def test_multiple_signals(cleanup):
|
||||||
async with database:
|
async with database:
|
||||||
async with database.transaction(force_rollback=True):
|
async with database.transaction(force_rollback=True):
|
||||||
|
|
||||||
@pre_save(Album)
|
@pre_save(Album)
|
||||||
async def before_save(sender, instance, **kwargs):
|
async def before_save(sender, instance, **kwargs):
|
||||||
await AuditLog(
|
await AuditLog(
|
||||||
@ -216,6 +219,7 @@ async def test_multiple_signals(cleanup):
|
|||||||
async def test_static_methods_as_signals(cleanup):
|
async def test_static_methods_as_signals(cleanup):
|
||||||
async with database:
|
async with database:
|
||||||
async with database.transaction(force_rollback=True):
|
async with database.transaction(force_rollback=True):
|
||||||
|
|
||||||
class AlbumAuditor:
|
class AlbumAuditor:
|
||||||
event_type = "ALBUM_INSTANCE"
|
event_type = "ALBUM_INSTANCE"
|
||||||
|
|
||||||
@ -240,6 +244,7 @@ async def test_static_methods_as_signals(cleanup):
|
|||||||
async def test_methods_as_signals(cleanup):
|
async def test_methods_as_signals(cleanup):
|
||||||
async with database:
|
async with database:
|
||||||
async with database.transaction(force_rollback=True):
|
async with database.transaction(force_rollback=True):
|
||||||
|
|
||||||
class AlbumAuditor:
|
class AlbumAuditor:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.event_type = "ALBUM_INSTANCE"
|
self.event_type = "ALBUM_INSTANCE"
|
||||||
@ -265,6 +270,7 @@ async def test_methods_as_signals(cleanup):
|
|||||||
async def test_multiple_senders_signal(cleanup):
|
async def test_multiple_senders_signal(cleanup):
|
||||||
async with database:
|
async with database:
|
||||||
async with database.transaction(force_rollback=True):
|
async with database.transaction(force_rollback=True):
|
||||||
|
|
||||||
@pre_save([Album, Cover])
|
@pre_save([Album, Cover])
|
||||||
async def before_save(sender, instance, **kwargs):
|
async def before_save(sender, instance, **kwargs):
|
||||||
await AuditLog(
|
await AuditLog(
|
||||||
@ -292,6 +298,7 @@ async def test_multiple_senders_signal(cleanup):
|
|||||||
async def test_modifing_the_instance(cleanup):
|
async def test_modifing_the_instance(cleanup):
|
||||||
async with database:
|
async with database:
|
||||||
async with database.transaction(force_rollback=True):
|
async with database.transaction(force_rollback=True):
|
||||||
|
|
||||||
@pre_update(Album)
|
@pre_update(Album)
|
||||||
async def before_update(sender, instance, **kwargs):
|
async def before_update(sender, instance, **kwargs):
|
||||||
if instance.play_count > 50 and not instance.is_best_seller:
|
if instance.play_count > 50 and not instance.is_best_seller:
|
||||||
|
|||||||
Reference in New Issue
Block a user