2

I have not been able to find a solution on SO that has worked for me yet, so I figured I would post a question.

I am running into issues migrating a database schema to a new PostgreSQL DB using django.

Here is the connection for my first DB, it works and I am able to run migrations perfectly fine. This is the DB I was using for testing intially, now I want to use a secondary DB and migrate the schema over.

DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql', 'HOST': '[HOST]', 'NAME': 'postgres', 'USER': 'postgres', 'PASSWORD': '[PW]', } } 

Here is the updated settings:

DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql', 'HOST': '[HOST]', 'NAME': 'test_mh', # this is only difference 'USER': 'postgres', 'PASSWORD': '[PW]', } } 

I run python3 manage.py makemigrations on the first connection and it is fine.

When I run the same command with the updated DB Name I get exceptions that state my tables do not exist.

 Traceback (most recent call last): File "/usr/local/lib/python3.6/dist-packages/django/db/backends/utils.py", line 85, in _execute return self.cursor.execute(sql, params) psycopg2.ProgrammingError: relation "objects_community" does not exist LINE 1: ...y"."state", "objects_community"."date_added" FROM "objects_c... ^ The above exception was the direct cause of the following exception: Traceback (most recent call last): File "manage.py", line 15, in <module> execute_from_command_line(sys.argv) File "/usr/local/lib/python3.6/dist-packages/django/core/management/__init__.py", line 381, in execute_from_command_line utility.execute() File "/usr/local/lib/python3.6/dist-packages/django/core/management/__init__.py", line 375, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/usr/local/lib/python3.6/dist-packages/django/core/management/base.py", line 316, in run_from_argv self.execute(*args, **cmd_options) File "/usr/local/lib/python3.6/dist-packages/django/core/management/base.py", line 350, in execute self.check() File "/usr/local/lib/python3.6/dist-packages/django/core/management/base.py", line 379, in check include_deployment_checks=include_deployment_checks, File "/usr/local/lib/python3.6/dist-packages/django/core/management/base.py", line 366, in _run_checks return checks.run_checks(**kwargs) File "/usr/local/lib/python3.6/dist-packages/django/core/checks/registry.py", line 71, in run_checks new_errors = check(app_configs=app_configs) File "/usr/local/lib/python3.6/dist-packages/django/core/checks/urls.py", line 40, in check_url_namespaces_unique all_namespaces = _load_all_namespaces(resolver) File "/usr/local/lib/python3.6/dist-packages/django/core/checks/urls.py", line 57, in _load_all_namespaces url_patterns = getattr(resolver, 'url_patterns', []) File "/usr/local/lib/python3.6/dist-packages/django/utils/functional.py", line 37, in __get__ res = instance.__dict__[self.name] = self.func(instance) File "/usr/local/lib/python3.6/dist-packages/django/urls/resolvers.py", line 533, in url_patterns patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module) File "/usr/local/lib/python3.6/dist-packages/django/utils/functional.py", line 37, in __get__ res = instance.__dict__[self.name] = self.func(instance) File "/usr/local/lib/python3.6/dist-packages/django/urls/resolvers.py", line 526, in urlconf_module return import_module(self.urlconf_name) File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "<frozen importlib._bootstrap>", line 994, in _gcd_import File "<frozen importlib._bootstrap>", line 971, in _find_and_load File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 665, in _load_unlocked File "<frozen importlib._bootstrap_external>", line 678, in exec_module File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed File "/home/viatech/Projects/ExactEstate/ExactEstate/ExactEstate/urls.py", line 21, in <module> path('adm/', include('interface_admin.urls')), File "/usr/local/lib/python3.6/dist-packages/django/urls/conf.py", line 34, in include urlconf_module = import_module(urlconf_module) File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "<frozen importlib._bootstrap>", line 994, in _gcd_import File "<frozen importlib._bootstrap>", line 971, in _find_and_load File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 665, in _load_unlocked File "<frozen importlib._bootstrap_external>", line 678, in exec_module File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed File "/home/viatech/Projects/ExactEstate/ExactEstate/interface_admin/urls.py", line 2, in <module> from interface_admin import views File "/home/viatech/Projects/ExactEstate/ExactEstate/interface_admin/views.py", line 4, in <module> from forms import RegistrationForm, SQLForm, SingleFileUploadForm, ApartmentTableFormBuilder File "/home/viatech/Projects/ExactEstate/ExactEstate/forms/ApartmentTableFormBuilder.py", line 10, in <module> class ApartmentTableFormBuilder(forms.Form): File "/home/viatech/Projects/ExactEstate/ExactEstate/forms/ApartmentTableFormBuilder.py", line 16, in ApartmentTableFormBuilder for community in Community.objects.all(): File "/usr/local/lib/python3.6/dist-packages/django/db/models/query.py", line 268, in __iter__ self._fetch_all() File "/usr/local/lib/python3.6/dist-packages/django/db/models/query.py", line 1186, in _fetch_all self._result_cache = list(self._iterable_class(self)) File "/usr/local/lib/python3.6/dist-packages/django/db/models/query.py", line 54, in __iter__ results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size) File "/usr/local/lib/python3.6/dist-packages/django/db/models/sql/compiler.py", line 1065, in execute_sql cursor.execute(sql, params) File "/usr/local/lib/python3.6/dist-packages/django/db/backends/utils.py", line 100, in execute return super().execute(sql, params) File "/usr/local/lib/python3.6/dist-packages/django/db/backends/utils.py", line 68, in execute return self._execute_with_wrappers(sql, params, many=False, executor=self._execute) File "/usr/local/lib/python3.6/dist-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers return executor(sql, params, many, context) File "/usr/local/lib/python3.6/dist-packages/django/db/backends/utils.py", line 85, in _execute return self.cursor.execute(sql, params) File "/usr/local/lib/python3.6/dist-packages/django/db/utils.py", line 89, in __exit__ raise dj_exc_value.with_traceback(traceback) from exc_value File "/usr/local/lib/python3.6/dist-packages/django/db/backends/utils.py", line 85, in _execute return self.cursor.execute(sql, params) django.db.utils.ProgrammingError: relation "objects_community" does not exist LINE 1: ...y"."state", "objects_community"."date_added" FROM "objects_c... 

1 Answer 1

2

The traceback is telling you that your form ApartmentTableFormBuilder is causing a query Community.objects.all() when the module loads.

When you run migrate or makemigrations on a new database, this causes an error since the objects_community table hasn't been created yet.

The correct solution is to modify your form so that it doesn't cause any queries when the module loads. A hackier solution would be to temporarily comment out the code that is causing the issue until you have run makemigrations and migrate for the first time.

Sign up to request clarification or add additional context in comments.

4 Comments

wonderful the commenting works! The ApartmentTableFormBuilder is a dynamic form used to allow the dynamic (i.e. non-model) creation of Apartment Tables in my DB. In that instance, I am not sure how it would be possible to not call Community.objects.all() as those objects are used to list the names of available Communities in a select option for an HTML form. This is a new feature though, I will look it over and see if I can recode the way things work to 'fix' this issue correctly. Either way, I am good to go now based on your post, so thanks!
It should be possible to rearrange your code so that the query does not run when the module loads. I can’t help any more than that because you didn’t show the code in your question.
Going to highjack this comment just to ask @Alasdair a question since you seem very knowledgeable about this type of thing. Consider the scenario where you have a big django projects and you have several apps that have models that have foriegn keys in other apps. IE: App1.ModelA has a FK of a model in App2 and App2.ModelB has a FK of a model in App1. What's the best way to deal with this when making migrations for a new DB? It gives circular dependency error if you just try running makemigrations. Thanks.
@SamCreamer that’s a different problem, it doesn’t belong as a comment on this answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.