﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
32369	CheckConstraints with pattern lookups and expressions as right-hand sides crash on Oracle, MySQL, and PostgreSQL.	Jaroslav Semančík	Simon Charette	"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."	Bug	closed	Database layer (models, ORM)	3.1	Normal	fixed		Ian Foote	Ready for checkin	1	0	0	0	0	0
