Skip to content

Commit

Permalink
Merge pull request #12 from M1hacka/issue-9
Browse files Browse the repository at this point in the history
Fixed issue 9
  • Loading branch information
M1ha-Shvn committed Jun 2, 2019
2 parents f64311b + b8f2f0d commit cee31c2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

setup(
name='django-pg-returning',
version='1.1.0',
version='1.1.1',
packages=['django_pg_returning'],
package_dir={'': 'src'},
url='https://github.com/M1hacka/django-pg-returning',
Expand Down
8 changes: 7 additions & 1 deletion src/django_pg_returning/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from django.db.models import sql, Field, QuerySet
from typing import Dict, Any, List, Type, Optional, Tuple

from django.db.models.query import EmptyResultSet

from .compatibility import chain_query, get_model_fields
from .queryset import ReturningQuerySet

Expand Down Expand Up @@ -65,7 +67,11 @@ def _get_returning_qs(self, query_type, values=None, **updates):
query.clear_ordering(force_empty=True)

self._result_cache = None
query_sql, query_params = query.get_compiler(self.db).as_sql()
try:
query_sql, query_params = query.get_compiler(self.db).as_sql()
except EmptyResultSet:
return ReturningQuerySet(None)

query_sql = query_sql + ' RETURNING %s' % field_str
with transaction.atomic(using=self.db, savepoint=False):
return ReturningQuerySet(query_sql, model=self.model, params=query_params, using=self.db,
Expand Down
10 changes: 10 additions & 0 deletions tests/test_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ def test_defer(self):
self.assertEqual(3 + i, item.int_field)
self.assertEqual(3 + i, item.id)

def test_empty_filter(self):
# This test originates from https://github.com/M1hacka/django-pg-returning/issues/9
res = TestModel.objects.filter(id__in=[]).update_returning(name='abc')
self.assertEqual(0, res.count())

def test_foreign_key_not_deferred(self):
# This test originates from https://github.com/M1hacka/django-pg-returning/issues/10

Expand Down Expand Up @@ -141,6 +146,11 @@ def test_defer(self):
self.assertEqual(3 + i, item.int_field)
self.assertEqual(3 + i, item.id)

def test_empty_filter(self):
# This test originates from https://github.com/M1hacka/django-pg-returning/issues/9
res = TestModel.objects.filter(id__in=[]).update_returning(name='abc')
self.assertEqual(0, res.count())

def test_foreign_key_not_deferred(self):
# This test originates from https://github.com/M1hacka/django-pg-returning/issues/10

Expand Down

0 comments on commit cee31c2

Please sign in to comment.