Skip to content

Commit 91be6e1

Browse files
authored
Fixed #34606 -- Fixed Right() function with zero length on Oracle and SQLite.
1 parent b0a6cc7 commit 91be6e1

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,7 @@ answer newbie questions, and generally made Django that much better:
545545
Justin Michalicek <[email protected]>
546546
Justin Myles Holmes <[email protected]>
547547
Jyrki Pulliainen <[email protected]>
548+
Kacper Wolkiewicz <[email protected]>
548549
Kadesarin Sanjek
549550
Kapil Bansal <[email protected]>
550551
Karderio <[email protected]>

django/db/models/functions/text.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,9 @@ class Right(Left):
276276

277277
def get_substr(self):
278278
return Substr(
279-
self.source_expressions[0], self.source_expressions[1] * Value(-1)
279+
self.source_expressions[0],
280+
self.source_expressions[1] * Value(-1),
281+
self.source_expressions[1],
280282
)
281283

282284

tests/db_functions/text/test_right.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
from django.db import connection
12
from django.db.models import IntegerField, Value
2-
from django.db.models.functions import Lower, Right
3+
from django.db.models.functions import Length, Lower, Right
34
from django.test import TestCase
45

56
from ..models import Author
@@ -26,6 +27,21 @@ def test_invalid_length(self):
2627
with self.assertRaisesMessage(ValueError, "'length' must be greater than 0"):
2728
Author.objects.annotate(raises=Right("name", 0))
2829

30+
def test_zero_length(self):
31+
Author.objects.create(name="Tom", alias="tom")
32+
authors = Author.objects.annotate(
33+
name_part=Right("name", Length("name") - Length("alias"))
34+
)
35+
self.assertQuerySetEqual(
36+
authors.order_by("name"),
37+
[
38+
"mith",
39+
"" if connection.features.interprets_empty_strings_as_nulls else None,
40+
"",
41+
],
42+
lambda a: a.name_part,
43+
)
44+
2945
def test_expressions(self):
3046
authors = Author.objects.annotate(
3147
name_part=Right("name", Value(3, output_field=IntegerField()))

0 commit comments

Comments
 (0)