0

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()
createdAt = models.DateTimeField(auto_now_add=True, blank=True)

I updated the above model to:

title = models CharField()
author = models.ForeignKey(User, on_delete=models.CASCADE)
introduction
 = models.charfield ()
body = models.TextField()
createdAt = models.DateTimeField(auto_now_add=True, blank=True)
    def get_absolute(self):
        return reverse ('post-details', kwarks={'pk': set.pk})

views.py

••••
class PostListView(Listview):
    model = Post
    template_name = 'start/start.html'
    context_object_name = 'posts'
    ordering = ['createdAt']

start.html

•••
{% for post in posts %}
    <h3>{{ post.title }}</h3>
    <i>{{ post.createdAt }} </I>
    <p>{{ post.introduction }}</p>
    <p>{{ post.body }}</p>
{% endfor %}
••••

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. As a newbie in Django I don't know where I'm getting it wrong.

3
  • 1
    Make sure to run python manage.py makemigrations and after this python manage.py migrate , after this run your server again Commented Jan 1, 2021 at 8:08
  • If it doesn't help, then update your question by adding traceback errors Commented Jan 1, 2021 at 8:12
  • I ran both commands (makemigrations and migrate), and the migration was successful. The error I'm getting is 1054, "Uknown column 'start_post.author_id' in the 'field list' Commented Jan 1, 2021 at 9:18

1 Answer 1

1

In django models even a small changes need to change the database(MySql , SQLite , Postgress , ... doesn't matter) so if you change it and dont run the commant python manage.py makemigrations and python manage.py migrate django will look for that in database which cant be find cause you havent changed it . so all you need run these code after changing in model

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.