You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When using django_db_views to generate database view migrations that rely on django.contrib.contenttypes.models.ContentType, running python manage.py makeviewmigrations --check --dry-run on a fresh database (without applying migrations) results in the following error:
psycopg.errors.UndefinedTable: relation "django_content_type" does not exist
This issue occurs because the auto-generated migration includes a query that references the django_content_type table, which does not exist in a freshly migrated database. Since CI/CD pipelines often use makeviewmigrations --check --dry-run to verify pending migrations without applying them, this leads to an unexpected failure.
Steps to Reproduce:
Install django_db_views in a Django project.
Create a DBView model that references ContentType in a query.
Allow django_db_views to auto-generate a migration that includes this model.
Run python manage.py makeviewmigrations --check --dry-run on a fresh database.
In this example, the view relies on ContentType.objects.get_for_model() to include the content_type_id in the query. The auto-generated migration from django_db_views will include a ViewRunPython operation with this query.
Auto-Generated Migration (example of what might be auto-generated)
fromdjango.dbimportmigrationsimportdjango_db_views.migration_functionsimportdjango_db_views.operationsclassMigration(migrations.Migration):
dependencies= [
("contenttypes", "0002_remove_content_type_name"), # added this by hand
]
operations= [
django_db_views.operations.ViewRunPython(
code=django_db_views.migration_functions.ForwardViewMigration(
'...'"example_db_view",
engine="django.db.backends.postgresql",
),
reverse_code=django_db_views.migration_functions.BackwardViewMigration(
"",
"example_db_view",
engine="django.db.backends.postgresql",
),
atomic=False,
),
]
Expected Behavior:
makeviewmigrations --check --dry-run should not fail if the contenttypes app has not yet been migrated.
Actual Behavior:
Running makeviewmigrations --check --dry-run results in a ProgrammingError because the django_content_type table is missing, as the database has not been migrated yet.
Workarounds Tried:
Ensuring that contenttypes is included in the migration dependencies (did not solve the issue).
Running migrate before makeviewmigrations --check --dry-run (works but defeats the purpose of checking migrations in CI).
Environment:
Django version: 4.2.16
Python version: 3.10.16
Database: PostgreSQL
django-db-views version: 0.1.9
psycopg version: 3.1.20
Additional Context:
This issue is particularly relevant in CI/CD workflows where the goal is to check for pending migrations without applying them. The current behavior prevents running makeviewmigrations --check --dry-run unless the database has already been migrated, which is counterproductive.
Would it be possible for django_db_views to modify the auto-generated migrations to account for cases where the django_content_type table doesn't exist, or offer a way to handle these queries differently in an unmigrated database?
The text was updated successfully, but these errors were encountered:
It’s also worth mentioning that running the following commands does not trigger the error:
python manage.py makemigrations --check --dry-run
python manage.py makemigrations
Both of these commands work fine, even in an unmigrated database. The error only occurs when using python manage.py makeviewmigrations or python manage.py makeviewmigrations --check --dry-run, which rely on the auto-generated migration views involving ContentType.
This distinction may help isolate the issue to the makeviewmigrations management command.
When using
django_db_views
to generate database view migrations that rely ondjango.contrib.contenttypes.models.ContentType
, runningpython manage.py makeviewmigrations --check --dry-run
on a fresh database (without applying migrations) results in the following error:This issue occurs because the auto-generated migration includes a query that references the
django_content_type
table, which does not exist in a freshly migrated database. Since CI/CD pipelines often usemakeviewmigrations --check --dry-run
to verify pending migrations without applying them, this leads to an unexpected failure.Steps to Reproduce:
django_db_views
in a Django project.DBView
model that referencesContentType
in a query.django_db_views
to auto-generate a migration that includes this model.python manage.py makeviewmigrations --check --dry-run
on a fresh database.Example Code:
DB View Model Using
ContentType
In this example, the view relies on
ContentType.objects.get_for_model()
to include thecontent_type_id
in the query. The auto-generated migration fromdjango_db_views
will include aViewRunPython
operation with this query.Auto-Generated Migration (example of what might be auto-generated)
Expected Behavior:
makeviewmigrations --check --dry-run
should not fail if thecontenttypes
app has not yet been migrated.Actual Behavior:
Running
makeviewmigrations --check --dry-run
results in aProgrammingError
because thedjango_content_type
table is missing, as the database has not been migrated yet.Workarounds Tried:
contenttypes
is included in the migration dependencies (did not solve the issue).migrate
beforemakeviewmigrations --check --dry-run
(works but defeats the purpose of checking migrations in CI).Environment:
4.2.16
3.10.16
django-db-views
version:0.1.9
psycopg
version:3.1.20
Additional Context:
This issue is particularly relevant in CI/CD workflows where the goal is to check for pending migrations without applying them. The current behavior prevents running
makeviewmigrations --check --dry-run
unless the database has already been migrated, which is counterproductive.Would it be possible for
django_db_views
to modify the auto-generated migrations to account for cases where thedjango_content_type
table doesn't exist, or offer a way to handle these queries differently in an unmigrated database?The text was updated successfully, but these errors were encountered: