Django数据库迁移错误(sqlite3-> postgre)

时间:2018-08-02 03:07:36

标签: django postgresql sqlite django-migrations

我已经使用django创建了一个应用,现在我正尝试使用另一个数据库,而不是默认的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...

问题出在哪里,我该怎么办?

1 个答案:

答案 0 :(得分:1)

尝试一下

运行python manage.py inspectdb 它将显示您迁移的模型,并将其与您的模型进行比较。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

它将解决迁移问题...