Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#33982 closed Bug (fixed)

TypeError creating DB table SQL with ExclusionConstraint as Statement returned instead of str

Reported by: James Beith Owned by: James Beith
Component: Database layer (models, ORM) Version: 4.1
Severity: Release blocker Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no
Pull Requests:16035 merged

Description

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 here as part of building the test database, a Statement object is returned, instead of a str, and we crash when attempting to join them here

Looks like the ExclusionConstraint switched to returning a Statement instead of a str in 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

Change History (7)

comment:1 by Mariusz Felisiak, 3 years ago

Severity: NormalRelease blocker
Triage Stage: UnreviewedAccepted

Thanks for the report! Regression in 0e656c02fe945389246f0c08f51c6db4a0849bd2.

by Mariusz Felisiak, 3 years ago

Attachment: test-33982.diff added

Regression test.

comment:2 by Mariusz Felisiak, 3 years ago

James, would you like to prepare a patch?

comment:3 by James Beith, 3 years ago

Mariusz, how's this? Hopefully it's this straight forward.

https://github.com/django/django/pull/16035

comment:4 by Mariusz Felisiak, 3 years ago

Has patch: set
Owner: changed from nobody to James Beith
Status: newassigned
Triage Stage: AcceptedReady for checkin

comment:5 by Mariusz Felisiak <felisiak.mariusz@…>, 3 years ago

Resolution: fixed
Status: assignedclosed

In 19e838da:

Fixed #33982 -- Fixed migrations crash when adding model with ExclusionConstraint.

Regression in 0e656c02fe945389246f0c08f51c6db4a0849bd2.

comment:6 by Mariusz Felisiak <felisiak.mariusz@…>, 3 years ago

In 7ba9a448:

[4.1.x] Fixed #33982 -- Fixed migrations crash when adding model with ExclusionConstraint.

Regression in 0e656c02fe945389246f0c08f51c6db4a0849bd2.

Backport of 19e838daa8872ee29fbea0bc471c2a6443f26835 from main

Note: See TracTickets for help on using tickets.
Back to Top