Skip to content

Stop updating on conflict if update_condition is False but not None #228

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion psqlextra/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,9 @@ def upsert(

self.on_conflict(
conflict_target,
ConflictAction.UPDATE,
ConflictAction.UPDATE
if (update_condition or update_condition is None)
else ConflictAction.NOTHING,
index_predicate=index_predicate,
update_condition=update_condition,
update_values=update_values,
Expand Down
30 changes: 30 additions & 0 deletions tests/test_upsert.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from django.db import connection, models
from django.db.models import F, Q
from django.db.models.expressions import CombinedExpression, Value
from django.test.utils import CaptureQueriesContext

from psqlextra.expressions import ExcludedCol
from psqlextra.fields import HStoreField
Expand Down Expand Up @@ -144,6 +145,35 @@ def test_upsert_with_update_condition():
assert obj1.active


@pytest.mark.parametrize("update_condition_value", [0, False])
def test_upsert_with_update_condition_false(update_condition_value):
"""Tests that an expression can be used as an upsert update condition."""

model = get_fake_model(
{
"name": models.TextField(unique=True),
"priority": models.IntegerField(),
"active": models.BooleanField(),
}
)

obj1 = model.objects.create(name="joe", priority=1, active=False)

with CaptureQueriesContext(connection) as ctx:
upsert_result = model.objects.upsert(
conflict_target=["name"],
update_condition=update_condition_value,
fields=dict(name="joe", priority=2, active=True),
)
assert upsert_result is None
assert len(ctx) == 1
assert 'ON CONFLICT ("name") DO NOTHING' in ctx[0]["sql"]

obj1.refresh_from_db()
assert obj1.priority == 1
assert not obj1.active


def test_upsert_with_update_values():
"""Tests that the default update values can be overriden with custom
expressions."""
Expand Down