Django migration: got relation does not exist or relation already exists errors
I tried to port a Diango app from one server to another and change database engine from sqllite3 to postgres. After I pulled the app from github to the new server and reconfigured database settings for postgres, I got a relation not existing error when I tried to migrate as shown below.
$ python manage.py migrate
Traceback (most recent call last):
File "/data/apps/anaconda3/envs/parts_env/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
psycopg2.errors.UndefinedTable: relation "parts" does not exist
LINE 1: SELECT "parts"."type" FROM "parts"
...
But after I created the table in the database and tried again, I got a relation already exists error as shown below.
$ python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, partsdb, sessions
Running migrations:
Applying partsdb.0001_initial...Traceback (most recent call last):
File "/data/apps/anaconda3/envs/parts_env/lib/python3.8/site-packages/django/db/backends/utils.py", line 82, in _execute
return self.cursor.execute(sql)
psycopg2.errors.DuplicateTable: relation "parts" already exists
...
So I'd get the "relation does not exist" error if I dropped the table and I'd get the "relation already exists" error if I created the table.
I omitted the tracing outputs which are from Django libraries and very lengthy, but I can supply them if they may help. Thanks.
I found the cause of the problems and was able to resolve the problems though I still don't know why the case. So in case some one might encounter the same kind problems, the cause is that there is a class defined in a file that accesses the database table to retrieve some data, as shown in the code snip below.
class PartsForm(forms.ModelForm):
for type in parts.objects.values_list('type'):
...
I wonder why codes in this file would get executed for migration. The table parts is the only table in the database. But anyway after I moved this file out of the app, the command "python manage.py migrate" ran and finished without errors (it generated all the tables in the database including the parts table).