Skip to content

Commit 1304953

Browse files
authored
Add offset clause for all Django versions (microsoft#117)
Issue microsoft#109, missing OFFSET clause causes select_for_update() to fail
1 parent 896a63c commit 1304953

File tree

4 files changed

+63
-2
lines changed

4 files changed

+63
-2
lines changed

mssql/compiler.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,9 @@ def as_sql(self, with_limits=True, with_col_aliases=False):
319319
# For subqueres with an ORDER BY clause, SQL Server also
320320
# requires a TOP or OFFSET clause which is not generated for
321321
# Django 2.x. See https://github.com/microsoft/mssql-django/issues/12
322-
if django.VERSION < (3, 0, 0) and not (do_offset or do_limit):
322+
# Add OFFSET for all Django versions.
323+
# https://github.com/microsoft/mssql-django/issues/109
324+
if not (do_offset or do_limit):
323325
result.append("OFFSET 0 ROWS")
324326

325327
# SQL Server requires the backend-specific emulation (2008 or earlier)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Generated by Django 4.0.3 on 2022-03-24 14:51
2+
3+
from django.db import migrations, models
4+
import django.db.models.deletion
5+
6+
7+
class Migration(migrations.Migration):
8+
9+
dependencies = [
10+
('testapp', '0018_choice_question'),
11+
]
12+
13+
operations = [
14+
migrations.CreateModel(
15+
name='Customer_name',
16+
fields=[
17+
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
18+
('Customer_name', models.CharField(max_length=100)),
19+
],
20+
options={
21+
'ordering': ['Customer_name'],
22+
},
23+
),
24+
migrations.CreateModel(
25+
name='Customer_address',
26+
fields=[
27+
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
28+
('Customer_address', models.CharField(max_length=100)),
29+
('Customer_name', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='testapp.customer_name')),
30+
],
31+
options={
32+
'ordering': ['Customer_address'],
33+
},
34+
),
35+
]

testapp/models.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Copyright (c) Microsoft Corporation.
22
# Licensed under the BSD license.
33

4+
import datetime
45
import uuid
56

67
from django import VERSION
@@ -200,3 +201,15 @@ class Choice(models.Model):
200201

201202
class Meta:
202203
unique_together = (('question', 'choice_text'))
204+
205+
206+
class Customer_name(models.Model):
207+
Customer_name = models.CharField(max_length=100)
208+
class Meta:
209+
ordering = ['Customer_name']
210+
211+
class Customer_address(models.Model):
212+
Customer_name = models.ForeignKey(Customer_name, on_delete=models.CASCADE)
213+
Customer_address = models.CharField(max_length=100)
214+
class Meta:
215+
ordering = ['Customer_address']

testapp/tests/test_fields.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,20 @@
33

44
from django.test import TestCase
55

6-
from ..models import UUIDModel
6+
from ..models import UUIDModel, Customer_name, Customer_address
77

88

99
class TestUUIDField(TestCase):
1010
def test_create(self):
1111
UUIDModel.objects.create()
12+
13+
14+
class TestOrderBy(TestCase):
15+
def test_order_by(self):
16+
# Issue 109
17+
# Sample: https://github.com/jwaschkau/django-mssql-issue109
18+
john = Customer_name.objects.create(Customer_name='John')
19+
Customer_address.objects.create(Customer_address='123 Main St', Customer_name=john)
20+
names = Customer_name.objects.select_for_update().all()
21+
addresses = Customer_address.objects.filter(Customer_address='123 Main St', Customer_name__in=names)
22+
self.assertEqual(len(addresses), 1)

0 commit comments

Comments
 (0)