How to Query data from MySQL database after updating model field in Django

Viewed 96

After updating posts model field in MySQL database using using Django framework, I'm unable to query posts from the Database. Whenever I run the command python manage.py runserver, all seems to be well with the server. However, when enter the post url to display the list of posts I uploaded on the database before updating the model field, I get 1054, "Uknown column 'start_post.author_id' in the 'field list' I have spent a couple of hours try to figure out why I'm getting the error but I still don't get it. In model.py I had:

....
title = models.CharField()
preamble= models.Charfield()
body = models.TextField()
....

I updated the above model to:

....
title = models.CharField()
author = models.ForeignKey(User, on_delete=models.CASCADE)
introduction = models.charfield()
body = models.TextField()
....

Before updating it everything was working appropriately. But after updating it I'm unable to access the list of posts on browser, from the url start\ as well as the post detail page. I didn't have the author field in the first model but I added it to the updated model. What can I do to solve this problem?

1 Answers

After update models, you have to run the commands:

python manage.py makemigrations

python manage.py migrate
  • makemigrations command will check the diffs between your current model in database and model in python code.

  • migrate will really run the migration to reflect your model with database model.

You can find more information about makemigrations and migrate in django documentation about makemigrations and migrate.

Related