Opened 4 years ago

Closed 4 years ago

#31399 closed Bug (fixed)

SchemaEditor._alter_field() looks for foreign key constraints even if the database doesn't support them.

Reported by: Tim Graham Owned by: Tim Graham
Component: Migrations Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

BaseDatabaseSchemaEditor._alter_field() does a lookup for foreign key constraints even if the database doesn't support them, resulting in test failures like this:

======================================================================
ERROR: test_alter_field_fk_keeps_index (schema.tests.SchemaTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/tim/code/django/tests/schema/tests.py", line 990, in test_alter_field_fk_keeps_index
    editor.alter_field(Book, old_field, new_field, strict=True)
  File "/home/tim/code/django/django/db/backends/base/schema.py", line 535, in alter_field
    old_db_params, new_db_params, strict)
  File "/home/tim/code/django/django/db/backends/base/schema.py", line 548, in _alter_field
    old_field.column,
ValueError: Found wrong number (0) of foreign key constraints for schema_book.author_id

This is a problem only when the argument strict=True is used (which I believe is only in tests).

To fix this, self.connection.features.supports_foreign_keys should be consulted.

Change History (4)

comment:1 by Mariusz Felisiak, 4 years ago

Easy pickings: set
Summary: SchemaEditor._alter_field() looks for foreign key constraints even if the database doesn't support themSchemaEditor._alter_field() looks for foreign key constraints even if the database doesn't support them.
Triage Stage: UnreviewedAccepted

comment:2 by Claude Paroz, 4 years ago

Has patch: set
Last edited 4 years ago by Tim Graham (previous) (diff)

comment:3 by Mariusz Felisiak, 4 years ago

Owner: changed from nobody to Tim Graham
Status: newassigned
Triage Stage: AcceptedReady for checkin

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

Resolution: fixed
Status: assignedclosed

In 8bebb8f1:

Fixed #31399 -- Made SchemaEditor._alter_field() ignore foreign key constraints if unsupported.

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