Skip to content

Fix Django 6.0 Autodetector mismatch errors with dynamic command inheritance#115

Closed
erdem wants to merge 2 commits intoxelixdev:mainfrom
yunojuno:misc/django-60-support
Closed

Fix Django 6.0 Autodetector mismatch errors with dynamic command inheritance#115
erdem wants to merge 2 commits intoxelixdev:mainfrom
yunojuno:misc/django-60-support

Conversation

@erdem
Copy link

@erdem erdem commented Jan 5, 2026

Problem

django-pgviews validates that both makemigrations and migrate use PGViewsAutodetector to properly detect view changes (see checks.py). When packages like django-linear-migrations are listed before django-pgviews in INSTALLED_APPS, their makemigrations command loads first without PGViewsAutodetector, causing the validation to fail:

  ?: (django_pgviews.W001) If you don't use PGViewsAutodetector on your migrate and
     makemigrations commands, django_pgviews will not detect and delete that views
     have been removed.
      HINT: makemigrations.Command.autodetector is MigrationAutodetector,
            migrate.Command.autodetector is PGViewsAutodetector.

Impact: Without PGViewsAutodetector on both commands, view removals and changes may not be detected properly in migrations.

Solution

Both commands now dynamically inherit from any custom command loaded by apps before django-pgviews:

def get_base_makemigrations_command():
    for app_config in apps.get_app_configs():
        if app_config.name == "django_pgviews":
            break
        try:
            return load_command_class(app_config.name, "makemigrations")
        except (ImportError, AttributeError):
            continue
    return DjangoMakeMigrationsCommand

class Command(get_base_makemigrations_command()):
    autodetector = PGViewsAutodetector

This ensures both commands use PGViewsAutodetector while preserving functionality from other packages.

Changes

  • makemigrations.py: Dynamic base class resolution
  • migrate.py: Dynamic base class resolution
  • pyproject.toml: Version 1.1.0

Copy link
Collaborator

@mikicz mikicz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ruff is suggesting these.

Otherwise looks ok!

Comment on lines 22 to 24
cmd = load_command_class(app_config.name, "makemigrations")
# Found a custom makemigrations command, use it as base
return cmd
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
cmd = load_command_class(app_config.name, "makemigrations")
# Found a custom makemigrations command, use it as base
return cmd
return load_command_class(app_config.name, "makemigrations")

Comment on lines 21 to 23
cmd = load_command_class(app_config.name, "migrate")
# Found a custom migrate command, use it as base
return cmd
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
cmd = load_command_class(app_config.name, "migrate")
# Found a custom migrate command, use it as base
return cmd
return load_command_class(app_config.name, "migrate")

@erdem
Copy link
Author

erdem commented Jan 5, 2026

Pushed another patch to cover up ruff complains and I have updated the PR description as well.

@erdem erdem requested a review from mikicz January 5, 2026 14:01
@erdem
Copy link
Author

erdem commented Jan 5, 2026

Just realized another issue with this PR patch. Also, this branch was only supposed to be used for fork testing.

Closing this PR for now. I'll create a new one after merging our main fork branch.

@erdem
Copy link
Author

erdem commented Jan 8, 2026

There is a better expiation why I have closed this PR in our fork repo PR description.

yunojuno#1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants