#32369: CheckConstraint with pattern lookup & another field crashes on 
PostgreSQL
-------------------------------------+-------------------------------------
               Reporter:  Jaroslav   |          Owner:  nobody
  Semančík                           |
                   Type:  Bug        |         Status:  new
              Component:  Database   |        Version:  3.1
  layer (models, ORM)                |
               Severity:  Normal     |       Keywords:
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 After fixing of #30408 a similar bug persists in case of indirect right-
 hand side, e.g. when comparing two model fields.

 Consider model:

 {{{
 class Foo(models.Model):
     foo = models.CharField(max_length=255)
     bar = models.CharField(max_length=255)

     class Meta:
         constraints = (
             models.CheckConstraint(
                 check=models.Q(foo__startswith=models.F('bar')),
                 name='check_foo_starts_with_bar',
             ),
         )
 }}}

 After making migration and running `sqlmigrate` with PostgreSQL backend we
 get `TypeError: not enough arguments for format string`.

 Traceback:

 {{{
 Traceback (most recent call last):
   File "/snap/pycharm-
 professional/228/plugins/python/helpers/pycharm/django_manage.py", line
 52, in <module>
     run_command()
   File "/snap/pycharm-
 professional/228/plugins/python/helpers/pycharm/django_manage.py", line
 46, in run_command
     run_module(manage_file, None, '__main__', True)
   File "/usr/lib/python3.9/runpy.py", line 210, in run_module
     return _run_module_code(code, init_globals, run_name, mod_spec)
   File "/usr/lib/python3.9/runpy.py", line 97, in _run_module_code
     _run_code(code, mod_globals, init_globals,
   File "/usr/lib/python3.9/runpy.py", line 87, in _run_code
     exec(code, run_globals)
   File "/home/giro/Projects/djangostartswith/manage.py", line 22, in
 <module>
     main()
   File "/home/giro/Projects/djangostartswith/manage.py", line 18, in main
     execute_from_command_line(sys.argv)
   File "/home/giro/src/django/core/management/__init__.py", line 401, in
 execute_from_command_line
     utility.execute()
   File "/home/giro/src/django/core/management/__init__.py", line 395, in
 execute
     self.fetch_command(subcommand).run_from_argv(self.argv)
   File "/home/giro/src/django/core/management/base.py", line 330, in
 run_from_argv
     self.execute(*args, **cmd_options)
   File "/home/giro/src/django/core/management/commands/sqlmigrate.py",
 line 29, in execute
     return super().execute(*args, **options)
   File "/home/giro/src/django/core/management/base.py", line 371, in
 execute
     output = self.handle(*args, **options)
   File "/home/giro/src/django/core/management/commands/sqlmigrate.py",
 line 65, in handle
     sql_statements = loader.collect_sql(plan)
   File "/home/giro/src/django/db/migrations/loader.py", line 345, in
 collect_sql
     state = migration.apply(state, schema_editor, collect_sql=True)
   File "/home/giro/src/django/db/migrations/migration.py", line 124, in
 apply
     operation.database_forwards(self.app_label, schema_editor, old_state,
 project_state)
   File "/home/giro/src/django/db/migrations/operations/models.py", line
 808, in database_forwards
     schema_editor.add_constraint(model, self.constraint)
   File "/home/giro/src/django/db/backends/base/schema.py", line 362, in
 add_constraint
     self.execute(sql)
   File "/home/giro/src/django/db/backends/base/schema.py", line 137, in
 execute
     self.collected_sql.append((sql % tuple(map(self.quote_value, params)))
 + ending)
 TypeError: not enough arguments for format string
 }}}

 The cause is again an unescaped `%` in the generated SQL.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/32369>
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/051.adb925349719af91ce41e699064458e4%40djangoproject.com.

Reply via email to