【问题标题】:Can't perform migration on django 3, python 3, sqlite3无法在 django 3、python 3、sqlite3 上执行迁移
【发布时间】:2021-02-08 16:54:36
【问题描述】:

我在我的 django 博客项目上运行 manage.py migrate 并得到这个:

(joelgoldstick.com) jcg@jcg-hp:~/code/p3/joelgoldstick.com/blog$ python manage.py migrate
.
.
.
django.db.utils.OperationalError: table "django_content_type" already exists

During handling of the above exception, another exception occurred:
.
.
.
    raise IntegrityError(
django.db.utils.IntegrityError: The row in table 'blog_app_entry_categories' with primary key '8' has an invalid foreign key: blog_app_entry_categories.category_id contains a value '3' that does not have a corresponding value in blog_category.id.

但表 blog_app_category 包含 id = 3 的行

这是表格:

sqlite> select id from blog_app_entry;
id
2
3
4
5
6
7
8
9
11
12
14
15
16
sqlite> select * from blog_app_category;
id|title|slug|description
1|Programming for fun|programming-fun|just a sample category.
2|Django|django|Anything related to django
3|python|python|general python 
4|tools|tools|various tools
5|Patty|patty|This is patty's stuff
6|Misc|misc|Things that don't fit in other categories
7|Vim|vim|About the Vim Editor
8|random|random|random category for testing
9|Deployment|deployment|deployment articles
10|git|git|git tips
sqlite> select * from blog_app_entry_categories;
id|entry_id|category_id
8|4|3
22|3|2
23|3|3
25|6|2
26|6|4
30|11|2
31|11|3
33|7|4
35|8|2
36|8|4
38|9|1
39|9|3
41|14|2
42|15|2
45|5|4
47|2|2
48|12|6
49|16|6

【问题讨论】:

    标签: python-3.x django sqlite


    【解决方案1】:

    在没有模型示例的情况下,不清楚该错误消息到底发生了什么,但是“blog_app_entry_categories.category_id”似乎与“blog_category.id”不同,所以只是因为存在 id=3 的行不能帮助我理解这个问题。如果我们能看到您的模型,可能会更清楚。

    否则,根据您已经创建的条目数量,您可以随时删除数据库和迁移文件并重新开始,过去当我弄乱了我的模型并且它们无法迁移时,这对我有用! (不要删除迁移文件夹中的 __init __.py 文件)。

    【讨论】:

    • 我添加了表格结果。我想我可以删除数据库并重新开始,但我宁愿不要丢失数据
    • 我在那些表中看不到任何与“blog_category”相关的内容,只有“blog_app_category”。哪里有错别字?我们也可以看看模型吗?
    • 这也让我感到困惑。没有 blog_category 表,即使回溯说有。如果您不介意,我将删除该问题,并以更好的方式提问
    • np 继续...不能先给我一个厚脸皮的接受吗? ;)
    猜你喜欢
    • 2012-07-02
    • 1970-01-01
    • 1970-01-01
    • 2021-06-27
    • 2017-05-17
    • 1970-01-01
    • 1970-01-01
    • 2019-09-06
    • 1970-01-01
    相关资源
    最近更新 更多