{{{
from django.db import models
from django.db.models import Q
class Person(models.Model):
firstname = models.CharField(max_length=50, blank=True, null=False,
default="")
class Meta:
constraints = [
models.CheckConstraint(
name="%(app_label)s_%(class)s_firstname",
check=(Q(firstname__exact="")),
)
]
}}}
Running `manage.py makemigrations` results in a migration with this
operation:
{{{
constraint=models.CheckConstraint(
check=models.Q(("firstname__exact", "")),
name="myapp_person_firstname",
),
}}}
And running `manage.py sqlmigrate myapp 0002` shows this (note there
should be a pair of empty quotes between `=` and `)` at the end):
{{{
ALTER TABLE `Person` ADD CONSTRAINT `myapp_person_firstname` CHECK
(`firstname` = );
}}}
Unsurprisingly, running the migration results in:
{{{
django.db.utils.ProgrammingError: (1064, "1064 (42000): You have an error
in your SQL syntax; check the manual that corresponds to your MariaDB
server version for the right syntax to use near ')' at line 1", '42000')
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/33802>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.