Django 数据库迁移错误(sqlite3->postgre)
Posted
技术标签:
【中文标题】Django 数据库迁移错误(sqlite3->postgre)【英文标题】:Django database migration error (sqlite3->postgre) 【发布时间】:2019-01-09 17:05:41 【问题描述】:我已经使用 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...
有什么问题,我该怎么办?
【问题讨论】:
你能分享你的models.py吗? 【参考方案1】:试试这个
运行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
它将解决迁移问题...
【讨论】:
我执行了inspectdb
命令,但它给了我这个:# This is an auto-generated Django model module. # You'll have to do the following manually to clean this up: # * Rearrange models' order # * Make sure each model has one field with primary_key=True # * Make sure each ForeignKey has on_delete set to the desired behavior. # * Remove managed = False lines if you wish to allow Django to create, modify, and delete the table # Feel free to rename the models, but don't rename db_table values or field names. from django.db import models
以上是关于Django 数据库迁移错误(sqlite3->postgre)的主要内容,如果未能解决你的问题,请参考以下文章
Django 迁移:sqlite3 开发数据库、Amazon Elastic Beanstalk 和 Amazon RDS postgresql 实时数据库
搭建 python 3.5+pycharm 2017.1.3+django 1.12.0 首次 将sqlite3 迁移到mysql