首页 > 解决方案 > Django 数据库迁移错误(sqlite3->postgre)

问题描述

我已经使用 django 创建了一个应用程序,现在我正在尝试使用 anther 数据库而不是它的默认值 sqlite3。我选择了postgres。

当我运行命令时

python3 manage.py makemigrations

我收到以下错误:

        Traceback (most recent call last):
          File "/root/mypro/env/lib/python3.5/site-packages/django/db/backends/utils.py", line 85, in _execute
            return self.cursor.execute(sql, params)
        psycopg2.ProgrammingError: relation "banners_supercat" does not exist
        LINE 1: ...supercat"."id", "banners_supercat"."english" FROM "banners_s...
                                                                     ^


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

        Traceback (most recent call last):
    ....
              File "/root/mypro/banners/urls.py", line 2, in <module>
            from . import views
          File "/root/mypro/banners/views.py", line 13, in <module>
            from .forms import (GoodBaseFormEn, GoodBaseFormFa, CreateSelectField, CreateFormDyn,
          File "/root/mypro/banners/forms.py", line 190, in <module>
            class CreateSearchFormDynEn(forms.Form):
          File "/root/mypro/banners/forms.py", line 191, in CreateSearchFormDynEn
            supercat = forms.ChoiceField(required=False, choices=[('','   ')]+getAllSuperCats(cv.I_EN), label='Category')
          File "/root/mypro/banners/queryfuncs.py", line 59, in getAllSuperCats
            return list(SuperCat.objects.all().values_list('id',('farsi', 'english')[lan]))
          File "/root/mypro/env/lib/python3.5/site-packages/django/db/models/query.py", line 272, in __iter__
            self._fetch_all()
          File "/root/mypro/env/lib/python3.5/site-packages/django/db/models/query.py", line 1179, in _fetch_all
            self._result_cache = list(self._iterable_class(self))
          File "/root/mypro/env/lib/python3.5/site-packages/django/db/models/query.py", line 138, in __iter__
            return compiler.results_iter(tuple_expected=True, chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
          File "/root/mypro/env/lib/python3.5/site-packages/django/db/models/sql/compiler.py", line 1019, in results_iter
            results = self.execute_sql(MULTI, chunked_fetch=chunked_fetch, chunk_size=chunk_size)
          File "/root/mypro/env/lib/python3.5/site-packages/django/db/models/sql/compiler.py", line 1068, in execute_sql
            cursor.execute(sql, params)
 ...
          File "/root/mypro/env/lib/python3.5/site-packages/django/db/utils.py", line 89, in __exit__
            raise dj_exc_value.with_traceback(traceback) from exc_value
          File "/root/mypro/env/lib/python3.5/site-packages/django/db/backends/utils.py", line 85, in _execute
            return self.cursor.execute(sql, params)
        django.db.utils.ProgrammingError: relation "banners_supercat" does not exist
        LINE 1: ...supercat"."id", "banners_supercat"."english" FROM "banners_s...

有什么问题,我该怎么办?

标签: djangopostgresqlsqlitedjango-migrations

解决方案


尝试这个

运行python manage.py inspectdb 它将显示您迁移的模型,将其与您的 models.py 进行比较

使您的 models.py 与 inspectdb 结果相同

之后做

python manage.py makemigrations
python manage.py migrate --fake <migrationfile_number> (eg:0002)

假迁移后,像以前一样更改models.py然后做

python manage.py makemigrations
python manage.py migrate

它将解决迁移问题...


推荐阅读