﻿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
33982	TypeError creating DB table SQL with ExclusionConstraint as Statement returned instead of str	James Beith	James Beith	"Whilst upgrading from Django 4.0.7 to 4.1 we've hit an exception attempting to run our test suit. 

We have some models with Postgres `ExclusionConstraint` constraints. When these constraints `constraint_sql()` is called [https://github.com/django/django/blob/4.1/django/db/backends/base/schema.py#L266 here] as part of building the test database, a `Statement` [https://github.com/django/django/blob/4.1/django/contrib/postgres/constraints.py#L116-L127 object is returned], instead of a str, and we crash when attempting to join them [https://github.com/django/django/blob/4.1/django/db/backends/base/schema.py#L271-L273 here]

Looks like the `ExclusionConstraint` switched to returning a `Statement` instead of a str in [https://github.com/django/django/pull/15235 this PR #15235].

Stacktrace below if it helps.

{{{
../../../../.virtualenvs/acme-corp/lib/python3.8/site-packages/pytest_django/plugin.py:465: in _django_db_marker
    request.getfixturevalue(""_django_db_helper"")
src/tests/conftest.py:966: in django_db_setup
    db_cfg = setup_databases(
../../../../.virtualenvs/acme-corp/lib/python3.8/site-packages/django/test/utils.py:220: in setup_databases
    connection.creation.create_test_db(
../../../../.virtualenvs/acme-corp/lib/python3.8/site-packages/django/db/backends/base/creation.py:78: in create_test_db
    call_command(
../../../../.virtualenvs/acme-corp/lib/python3.8/site-packages/django/core/management/__init__.py:198: in call_command
    return command.execute(*args, **defaults)
src/acme-corp/interfaces/common/management_commands.py:50: in execute
    return super().execute(*args, **options)
../../../../.virtualenvs/acme-corp/lib/python3.8/site-packages/django/core/management/base.py:448: in execute
    output = self.handle(*args, **options)
src/acme-corp/interfaces/systemjobs/management/commands/migrate.py:31: in handle
    super().handle(*args, **options)
../../../../.virtualenvs/acme-corp/lib/python3.8/site-packages/pytest_django/fixtures.py:276: in handle
    return super().handle(*args, **kwargs)
../../../../.virtualenvs/acme-corp/lib/python3.8/site-packages/django/core/management/base.py:96: in wrapped
    res = handle_func(*args, **kwargs)
../../../../.virtualenvs/acme-corp/lib/python3.8/site-packages/django/core/management/commands/migrate.py:314: in handle
    self.sync_apps(connection, executor.loader.unmigrated_apps)
../../../../.virtualenvs/acme-corp/lib/python3.8/site-packages/django/core/management/commands/migrate.py:476: in sync_apps
    editor.create_model(model)
../../../../.virtualenvs/acme-corp/lib/python3.8/site-packages/django/db/backends/base/schema.py:446: in create_model
    sql, params = self.table_sql(model)
../../../../.virtualenvs/acme-corp/lib/python3.8/site-packages/django/db/backends/base/schema.py:272: in table_sql
    ""definition"": "", "".join(
E   TypeError: sequence item 9: expected str instance, Statement found
}}}
"	Bug	closed	Database layer (models, ORM)	4.1	Release blocker	fixed			Ready for checkin	1	0	0	0	0	0
