xelixdev / django-pgviews-redux

Fork of django-postgres that focuses on maintaining and improving support for Postgres SQL Views.
The Unlicense
64 stars 15 forks source link

Materialized view with schema and index fails to create #13

Closed tobiasmcnulty closed 2 years ago

tobiasmcnulty commented 2 years ago

When defining a materialized view like this:

class MyMatView(pg.MaterializedView):
    sql = lazy(get_view_sql, str)()

    record_id = models.IntegerField()
    mod_date = models.DateTimeField()
    # <snip>

    class Meta:
        db_table = "reporting.my_table"
        managed = False
        indexes = [
            models.Index(fields=["record_id"]),
        ]

I receive the following exception when running migrate:

Traceback (most recent call last):
  File "<snip>/manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "<snip>/django/core/management/__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "<snip>/django/core/management/__init__.py", line 413, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "<snip>/django/core/management/base.py", line 354, in run_from_argv
    self.execute(*args, **cmd_options)
  File "<snip>/django/core/management/base.py", line 398, in execute
    output = self.handle(*args, **options)
  File "<snip>/django/core/management/base.py", line 89, in wrapped
    res = handle_func(*args, **kwargs)
  File "<snip>/django/core/management/commands/migrate.py", line 268, in handle
    emit_post_migrate_signal(
  File "<snip>/django/core/management/sql.py", line 42, in emit_post_migrate_signal
    models.signals.post_migrate.send(
  File "<snip>/django/dispatch/dispatcher.py", line 180, in send
    return [
  File "<snip>/django/dispatch/dispatcher.py", line 181, in <listcomp>
    (receiver, receiver(signal=self, sender=sender, **named))
  File "<snip>/django_pgviews/apps.py", line 34, in sync_pgviews
    vs.run(
  File "<snip>/django_pgviews/models.py", line 41, in run
    if super().run(
  File "<snip>/django_pgviews/models.py", line 27, in run
    backlog = self.run_backlog(backlog, **kwargs)
  File "<snip>/django_pgviews/models.py", line 77, in run_backlog
    status = create_materialized_view(
  File "/usr/lib/python3.9/contextlib.py", line 79, in inner
    return func(*args, **kwds)
  File "<snip>/django_pgviews/view.py", line 224, in create_materialized_view
    schema_editor.add_index(view_cls, index)
  File "<snip>/django/db/backends/postgresql/schema.py", line 218, in add_index
    self.execute(index.create_sql(model, self, concurrently=concurrently), params=None)
  File "<snip>/django/db/backends/base/schema.py", line 145, in execute
    cursor.execute(sql, params)
  File "<snip>/django/db/backends/utils.py", line 98, in execute
    return super().execute(sql, params)
  File "<snip>/django/db/backends/utils.py", line 66, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "<snip>/django/db/backends/utils.py", line 75, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "<snip>/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "<snip>/django/db/utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "<snip>/django/db/backends/utils.py", line 82, in _execute
    return self.cursor.execute(sql)
django.db.utils.ProgrammingError: relation "reporting.my_table" does not exist

Removing the schema from db_table allows migrate to complete successfully.

mikicz commented 2 years ago

Hi Tobias!

My guess is that the index is blissfully unaware that the materialized view got created in a different schema. Thanks for the report, I will take a look

mikicz commented 2 years ago

Sorry, kind of forgot to actually merge and release it, the version on PyPI should fix this https://pypi.org/project/django-pgviews-redux/0.8.3/