table blog_post has no column named author_id

Hello Django Forum!

I have a project that I am working on and the error message reads

OperationalError at /admin/blog/post/add/

table blog_post has no column named author_id

Request Method: POST
Request URL: http://127.0.0.1:8000/admin/blog/post/add/
Django Version: 4.2.3
Exception Type: OperationalError
Exception Value: table blog_post has no column named author_id

Here is my models.py

from django.db import models
from django.urls import reverse 

# Create your models here.

class Post(models.Model):
    # primary key automatically placed here auto incrementing.
    title = models.CharField(max_length=200)
    author = models.ForeignKey(
        "auth.User",
        on_delete=models.CASCADE,
    )
    body = models.TextField()

    # this function will make a human readable version of the title field of any blog post.
    def __str__(self):
        return self.title
    
    """We also add a get_absolute_url() method, which we haven't seen before, that tells Django how
    to calculate the canonical URL for our model object. It says to use the URL named post_detail and pass in the pk"""
    
    def get_absolute_url(self): # new 
        return reverse("post_detail", kwargs={"pk": self.pk})
    

I thought django gave id’s when they created tables. If I want to create an author id column for table_blog post how would I do that?

Per the instructions it says that I will have an error if I DO NOT enter in an author name and that I can set the values to null.

When I access the Django admin interface to add a post it has me fill in a title box, an author box, and an body box. when i select the drop down for author it does not let me create a new author, it instead tells me I can add in users as authors. I try to complete the process of filling in the fields and using a user and it results in an error.

Did you run makemigrations and migrate after adding this column to your model?

Also in the future, if you’re requesting assistance with an error you are receiving, please post the complete error message with the traceback from the console where you’re running Django and not the excerpt from the browser page showing the error.

1 Like

Drats. One minute I fail to post the error message at all, than I post the wrong error message, I am sorry I will try to create a step by step sheet to reference before posting on django chat.

Whenever I make a change to my models.py I need to make a migration? It seems I need to write that fact down when I troubleshoot.

Your solution worked, thank you Ken!

Yes, it’s documented in the tutorial

See Writing your first Django app, part 2 | Django documentation | Django