Django Sqlite error, "Table has no column named_id"

Hi everyone,
I am learning Django and trying to develop a small website. This Website has many users and every user can create posts as well as can comment other posts too. Because of that I created three models: My_User, Post, Comment. When when try to add a comment on a Post, I got the error : “OperationalError at /register/posts/Table
table Register_comment has no column named author_id”. Even in Shell I am getting the same error!
However; the user can add new Post.
I tried a lot to solve but it was not possible. I hope someone can help me. Thanks in Advance.
Down I added my models.py and views.py:

#models.py:
class My_User(models.Model):
    # user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='my_user',default="")
    username = models.CharField(max_length=50, blank=True)
    nationality = models.CharField(max_length=50)
    email = models.CharField(max_length=50, blank=True)
    is_active = models.BooleanField(blank=True, default=False)
    email_verified = models.BooleanField(blank=True, default=False)
    image = models.ImageField(upload_to='images/', blank=True)

    def save(self, *args, **kwargs):
        return super().save(*args, **kwargs)

    def __str__(self):
        # return f"{self.username} ({self.nationality})"
        return self.username

class Post(models.Model):
    title = models.CharField(max_length=150)
    content = models.TextField(validators=[MinLengthValidator(10)])
    author = models.ForeignKey(My_User, on_delete=models.SET_NULL, null=True, related_name="posts")
    image = models.ImageField(upload_to="posts", null=True)


    def __str__(self):
        return f"{self.title} {self.author}"

class Comment(models.Model):
    author = models.ForeignKey(My_User,null=True, on_delete=models.CASCADE, related_name="commentsauthor")
    comment_text = models.TextField(max_length=450, null=True,default="")
    post = models.ForeignKey(Post, on_delete=models.CASCADE, related_name="comments")

    def save(self, *args, **kwargs):
        return super().save(*args, **kwargs)

#views.py
class PostDetailView(View):
    def get(self, request, title):
        post = get_object_or_404(Post, title=title)
        comment_form = CommentForm()
        return render(request, 'Register/post_detail.html', {
            'post': post,
            'comment_form': comment_form,
        })
    def post(self, request, title):
        # current_user = My_User.objects.get(username=request.user)

        post = get_object_or_404(Post, title=title)
        comment_form = CommentForm(request.POST)
        if comment_form.is_valid():
            print("############################################NOT Valid")
            current_user = get_object_or_404(My_User, username=request.user)
            comment = comment_form.save(commit=False)
            hp = Comment(author=current_user,
                      comment_text=comment_form.cleaned_data['comment_text'],
                      post=post
                     )
            hp.save()
            return HttpResponseRedirect(reverse("post-detail-page", args=[title]))

        context = {
            "post": post,
            "comment_form": comment_form,
        }
        return render(request, "Register/post_detail.html", context)

<!--post_detail.html -->

{% extends "base.html" %}
{% load static %}
{% block title %} Post Details {% endblock %}
{% block css_files %}
<link rel="stylesheet" href="{% static "Register/post_detail.css"  %}">
{% endblock %}
{% block content %}
    <section class="container">
        <h1>{{ post.title }}</h1>
        <p class="post-content">{{ post.content }}</p>
        <img src="{{ post.image.url }}" alt="{{ post.author.username }}" class="img-fluid user-image">
        <div class="author-info">
            <p><a href="{% url 'user-detail-page' post.author.username %}">Author: {{ post.author }}</a> </p>
        </div>
        <div class="author-info">
            <p>Logged in User: {{ user.username }}</p>
        </div>
        <!-- Add more details as needed -->
    </section>
<section id="comment-form">
<h2>Your Comment</h2>
    <form action="{% url "post-detail-page" post.title %}" method="POST">
        {% csrf_token %}
        {% for form_field in comment_form %}
            <div class="my-form-control {% if form_field.errors %}invalid{% endif %}" >
                {{ form_field.label_tag }}
                {{ form_field }}
                {{ form_field.errors }}
            </div>
        {% endfor %}
        <button> Save Comment </button>
    </form>
</section>
{% endblock %}

Side note: When posting code or templates here, enclose the code between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```. This forces the forum software to keep your code properly formatted. (I’ve taken the liberty of modifying your original post to fix this.)

The most likely causes of this are either that you have made a model definition change and have not yet run makemigrations and migrate, or, something else has been done to corrupt the database.

First step is to run makemigrations and then migrate. If that doesn’t show any changes needing to be made/applied, then the next step is to compare your database schema with your models.

1 Like

Thank you.
Actually I had to mention this, I did many time s “python manage.py makemigrations” and “python manage.py migrate” but nothing changed. Still get same error

In that case, since you’re using sqlite, I’d try creating a new database, then makemigrations / migrate to see if the new database is “right”.

It may also be helpful if you posted the full traceback from the console for this error.

I created a new sqlite and it’s working now. I hv error debugging from browser but I don’t post it here.
I can send it to you if you want.

No, at this point I suggest you use your new db. It’s not really worth trying to figure out what happened to the old file.

Side note:

For future reference, it’s usually more useful to post the traceback from the console where you run runserver instead of the browser output. But we don’t need either one at this point.