joel goldstick
joel goldstick

Reputation: 4493

Can't perform migration on django 3, python 3, sqlite3

I run manage.py migrate on my django blog project and get this:

(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.

but the table blog_app_category contains a row with id = 3

Here are the tables:

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

Upvotes: 1

Views: 362

Answers (1)

nihilok
nihilok

Reputation: 2113

It's unclear exactly what's going on from that error message without examples of your models but, "blog_app_entry_categories.category_id" doesn't appear to be the same as "blog_category.id", so just because the row with id=3 exists doesn't help me understand the issue. It might be clearer if we can see your models.

Otherwise, depending on how many entries you've already created, you could always delete your database and migrations files and start again, has worked for me in the past when I've messed up my models and they can't migrate! (don't delete the __ init __.py files in the migrations folders).

Upvotes: 1

Related Questions