#32635: Creating a UniqueConstraint with condition referencing OneToOneField
generates invalid SQL
-------------------------------------+-------------------------------------
               Reporter:  sim1234    |          Owner:  nobody
                   Type:             |         Status:  new
  Uncategorized                      |
              Component:             |        Version:  2.2
  Uncategorized                      |       Keywords:  UniqueConstraint
               Severity:  Normal     |  OneToOneField
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 == Creating a UniqueConstraint with condition referencing OneToOneField
 generates invalid SQL

 Example:
 {{{
 # models.py
 from django.db import models

 class Model1(models.Model):
     name = models.CharField(max_length=255)

     class Meta:
         constraints = (
             models.UniqueConstraint(
                 name="unique_onetoone",
                 fields=("name", ),
                 condition=models.Q(model__isnull=True),
             ),
         )

 class Model2(models.Model):
     name = models.CharField(max_length=255)
     model = models.OneToOneField(
         Model1, related_name="model", on_delete=models.CASCADE
     )


 # generated migrations/0001_initial.py
 import django.db.models.deletion
 from django.db import migrations, models


 class Migration(migrations.Migration):
     dependencies = []

     operations = [
         // ... CreateModel, etc ...
         migrations.AddConstraint(
             model_name="model1",
             constraint=models.UniqueConstraint(
                 name="unique_onetoone",
                 fields=("name", ),
                 condition=models.Q(model__isnull=True),
             ),
         ),
     ]
 }}}




 Running ''manage.py sqlmigrate test 0001'' generates this SQL for creating
 the unique index and the WHERE clause is completly wrong.


 {{{
 -- ... CREATE TABLE, etc ...
 CREATE UNIQUE INDEX "unique_onetoone" ON "test_model1" ("name", ) WHERE
 "id" IS NULL;
 }}}


 Expected behaviour is receiving ''FieldError("Joined field references are
 not permitted in this query")''. This behaviour is observed when using
 ''condition=models.Q(model__id__isnull=True)''.

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

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/050.c49917133938a651af9deb9be40f6edb%40djangoproject.com.

Reply via email to