[Django] #34505: Non-deterministic collations doesn't work with unique related fields on PostgreSQL

1 view
Skip to first unread message

Django

unread,
Apr 20, 2023, 9:14:33 AM4/20/23
to django-...@googlegroups.com
#34505: Non-deterministic collations doesn't work with unique related fields on
PostgreSQL
-------------------------------------+-------------------------------------
Reporter: Petter | Owner: nobody
Friberg |
Type: Bug | Status: new
Component: | Version: 4.2
Migrations | Keywords: collation unique
Severity: Normal | related index
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
Creating a relation to a column with a non-deterministic collation raises:

`django.db.utils.NotSupportedError: nondeterministic collations are not
supported for operator class "varchar_pattern_ops"`

Error is raised due to creation of a `_like` index.

This is kind of a continuation of #33901, but instead regarding relations.

Here's some code to reproduce:

{{{
#!python

class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
CreateCollation(
'ci',
provider='icu',
locale='und-u-ks-level2',
deterministic=False
),
migrations.CreateModel(
name='User',
fields=[
('id', models.BigAutoField(auto_created=True,
primary_key=True, serialize=False, verbose_name='ID')),
('password', models.CharField(max_length=128,
verbose_name='password')),
('username', models.CharField(db_collation='ci',
max_length=30, unique=True, verbose_name='username')),
],
options={
'abstract': False,
},
),
migrations.CreateModel(
name='ExtendedUser',
fields=[
('id', models.BigAutoField(auto_created=True,
primary_key=True, serialize=False, verbose_name='ID')),
('user',
models.OneToOneField(on_delete=django.db.models.deletion.CASCADE,
to=settings.AUTH_USER_MODEL, to_field='username')),
],
),
]
}}}

Adding a test case similar to what can be found in
[changeset:"f3f9d03edf17ccfa17263c7efa0b1350d1ac9278" f3f9d03], but
adjusted to include a `OneToOneField`, fails on main

{{{
#!python
@isolate_apps("schema")
@unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL
specific")
@skipUnlessDBFeature(
"supports_collation_on_charfield",
"supports_non_deterministic_collations",
)
def test_unique_relation_to_collation_charfield(self):
ci_collation = "case_insensitive"

def drop_collation():
with connection.cursor() as cursor:
cursor.execute(f"DROP COLLATION IF EXISTS {ci_collation}")

self.addCleanup(drop_collation)

with connection.cursor() as cursor:
cursor.execute(
f"CREATE COLLATION IF NOT EXISTS {ci_collation} (provider
= icu, "
f"locale = 'und-u-ks-level2', deterministic = false)"
)

class CiCharModel(Model):
field = CharField(max_length=16, db_collation=ci_collation,
unique=True)

class Meta:
app_label = "schema"

class RelationModel(Model):
field = OneToOneField(CiCharModel, on_delete=CASCADE,
to_field="field")

class Meta:
app_label = "schema"

# Create the table.
with connection.schema_editor() as editor:
editor.create_model(CiCharModel)
editor.create_model(RelationModel)

self.isolated_local_models = [CiCharModel, RelationModel]
self.assertEqual(
self.get_column_collation(RelationModel._meta.db_table,
"field_id"),
ci_collation,
)
self.assertIn("field_id",
self.get_uniques(RelationModel._meta.db_table))
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/34505>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Apr 20, 2023, 9:29:26 AM4/20/23
to django-...@googlegroups.com
#34505: Non-deterministic collations doesn't work with unique related fields on
PostgreSQL
-------------------------------------+-------------------------------------
Reporter: Petter Friberg | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 4.2
Severity: Normal | Resolution:
Keywords: collation unique | Triage Stage:
related index | Unreviewed
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Petter Friberg):

* has_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/34505#comment:1>

Django

unread,
Apr 21, 2023, 2:50:28 AM4/21/23
to django-...@googlegroups.com
#34505: Non-deterministic collations doesn't work with unique related fields on
PostgreSQL
-------------------------------------+-------------------------------------
Reporter: Petter Friberg | Owner: Petter
| Friberg
Type: Bug | Status: assigned

Component: Migrations | Version: 4.2
Severity: Normal | Resolution:
Keywords: collation unique | Triage Stage: Accepted
related index |

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* owner: nobody => Petter Friberg
* status: new => assigned
* stage: Unreviewed => Accepted


Comment:

Thanks for the report.

--
Ticket URL: <https://code.djangoproject.com/ticket/34505#comment:2>

Django

unread,
Apr 21, 2023, 4:32:20 AM4/21/23
to django-...@googlegroups.com
#34505: Non-deterministic collations doesn't work with unique related fields on
PostgreSQL
-------------------------------------+-------------------------------------
Reporter: Petter Friberg | Owner: Petter
| Friberg
Type: Bug | Status: assigned
Component: Migrations | Version: 4.2
Severity: Normal | Resolution:
Keywords: collation unique | Triage Stage: Ready for
related index | checkin

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/34505#comment:3>

Django

unread,
Apr 21, 2023, 5:37:50 AM4/21/23
to django-...@googlegroups.com
#34505: Non-deterministic collations doesn't work with unique related fields on
PostgreSQL
-------------------------------------+-------------------------------------
Reporter: Petter Friberg | Owner: Petter
| Friberg
Type: Bug | Status: closed
Component: Migrations | Version: 4.2
Severity: Normal | Resolution: fixed

Keywords: collation unique | Triage Stage: Ready for
related index | checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak <felisiak.mariusz@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"8ed25d65ea7546fafd808086fa07e7e5bb5428fc" 8ed25d6]:
{{{
#!CommitTicketReference repository=""
revision="8ed25d65ea7546fafd808086fa07e7e5bb5428fc"
Fixed #34505 -- Skipped varchar_pattern_ops/text_pattern_ops index
creation when db_collation is set in related field.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/34505#comment:4>

Reply all
Reply to author
Forward
0 new messages