Mismatched local/remote models

I have a model catalog with the table section. Apps (including admin) on the remote server throw the error relation "catalog_section does not exist when accessing that table; but they don’t on the local server.

When I run locally, I get You have 1 unapplied migration(s). Running makemigrations or migrate throws the error KeyError: 'menu_items'.

When I deploy to Heroku, the log says there are no migrations to apply.

I think there might be some mixup with terminology here.

What your description seems to imply here is that you have an app named catalog with a model named section. Is that correct?

What do you mean by “local” and “remote” servers here? Are you talking about database instances or Django instances?

What changes have you made to your system since the last time it was working?

Hopefully, you’re only running makemigrations in your development environment and pushing your code with the migration files to each environment. You should only be running migrate when you deploy.

Also, when requesting assistance with error messages, we request that you post the complete error message with the full traceback from the server console.

What your description seems to imply here is that you have an app named catalog with a model named section . Is that correct?

Yup, sorry.

What do you mean by “local” and “remote” servers here? Are you talking about database instances or Django instances?

Local: running on my local machine with local Postgres db; remote: deployed on Heroku with remote Postgres db.

What changes have you made to your system since the last time it was working?

I hadn’t done anything with the code or db for months; just recently I noticed that I’d made a number of changes that I hadn’t committed or pushed; and that there were a number of un-applied migrations, which I applied.

Hopefully, you’re only running makemigrations in your development environment and pushing your code with the migration files to each environment. You should only be running migrate when you deploy.

I had deployed without first committing the migrations, which may be where the problems started; later I committed/pushed the migrations, then deployed again.

Error when launched on local instance:

You have 2 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): catalog.
Run 'python manage.py migrate' to apply them.

Error when accessing a page:

Internal Server Error: /admin/catalog/section/
Traceback (most recent call last):
File “/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/django/db/backends/utils.py”, line 105, in _execute
return self.cursor.execute(sql, params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/psycopg/cursor.py”, line 97, in execute
raise ex.with_traceback(None)
psycopg.errors.UndefinedColumn: column catalog_section.foo does not exist
LINE 1: …ction".“path”, “catalog_section”.“display_order”, "catalog_s…
^

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File “/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/django/core/handlers/exception.py”, line 55, in inner
response = get_response(request)
^^^^^^^^^^^^^^^^^^^^^
File “/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/django/core/handlers/base.py”, line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/django/contrib/admin/options.py”, line 716, in wrapper
return self.admin_site.admin_view(view)(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/django/utils/decorators.py”, line 188, in _view_wrapper
result = _process_exception(request, e)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/django/utils/decorators.py”, line 186, in _view_wrapper
response = view_func(request, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/django/views/decorators/cache.py”, line 80, in _view_wrapper
response = view_func(request, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/django/contrib/admin/sites.py”, line 240, in inner
return view(request, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/django/utils/decorators.py”, line 48, in _wrapper
return bound_method(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/django/utils/decorators.py”, line 188, in _view_wrapper
result = _process_exception(request, e)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/django/utils/decorators.py”, line 186, in _view_wrapper
response = view_func(request, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/django/contrib/admin/options.py”, line 2126, in changelist_view
“selection_note”: _(“0 of %(cnt)s selected”) % {“cnt”: len(cl.result_list)},
^^^^^^^^^^^^^^^^^^^
File “/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/django/db/models/query.py”, line 382, in len
self._fetch_all()
File “/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/django/db/models/query.py”, line 1928, in _fetch_all
self._result_cache = list(self._iterable_class(self))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/django/db/models/query.py”, line 91, in iter
results = compiler.execute_sql(
^^^^^^^^^^^^^^^^^^^^^
File “/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/django/db/models/sql/compiler.py”, line 1562, in execute_sql
cursor.execute(sql, params)
File “/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/django/db/backends/utils.py”, line 122, in execute
return super().execute(sql, params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/django/db/backends/utils.py”, line 79, in execute
return self._execute_with_wrappers(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/django/db/backends/utils.py”, line 92, in _execute_with_wrappers
return executor(sql, params, many, context)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/django/db/backends/utils.py”, line 100, in _execute
with self.db.wrap_database_errors:
File “/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/django/db/utils.py”, line 91, in exit
raise dj_exc_value.with_traceback(traceback) from exc_value
File “/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/django/db/backends/utils.py”, line 105, in _execute
return self.cursor.execute(sql, params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/psycopg/cursor.py”, line 97, in execute
raise ex.with_traceback(None)
django.db.utils.ProgrammingError: column catalog_section.foo does not exist
LINE 1: …ction".“path”, “catalog_section”.“display_order”, "catalog_s…

If you’ve got two different issues with two different environments, then you want to separate these into two separate questions to avoid confusion. Pick one of the two to continue the immediate discussion and then we can address the other after this is done.

But in the general case, if you have deleted, edited, or overwritten any migration files, the easiest answer is going to be for you do drop the database and recreate it.

If you’ve got two different issues with two different environments

The issue is only with the Heroku environment.

the easiest answer is going to be for you do drop the database and recreate it.

Well, hardly easy.

The remaining problem is with one model; running migrate, I get psycopg.errors.DuplicateTable: relation "catalog_section" already exists. There’s got to be some way to fix that one migration. Perhaps it would be easier to delete that one model (“Section”) and recreate it; it doesn’t have that much data in it.

Note: I didn’t say it was easy. I said it was the easiest answer. Compared to the alternatives, it’s generally going to be the quickest solution as well. Back up the data, drop the database, apply the migrations to recreate the tables, restore the data. (You may need to deal with data issues for those models that don’t match.)

The issue is that your Django migration table isn’t in sync with the migration files in the project. Yes, it can be fixed, if you know what you’re doing and can work through the issues that may come up along the way. But a “quick fix” that doesn’t address all the possible underlying issues could just be deferring problems that might come back to bite you later.

Thanks.

What if it’s the migrations that are the problem?

Then you’ll discover that when you try to run migrate. At that point, since you won’t have a database anyway, you can delete all the migration files and recreate them.