Ryan Oh
Ryan Oh

Reputation: 647

OperationalError : "no such column" when using ForeignKey in Django

so I'm trying to make a user-specific page with Django. In my models.py file, I have this code:

class ToDoList(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="todolist", null=True)
    name = models.CharField(max_length = 200)

    def __str__(self):
        return self.name

But I keep getting this "no such column: main_todolist.user_id" error.

I'm aware that when using ForeignKey, Django automatically creates a column named '..._id' as I do the makemigrations and migrate command.
I've also tried deleting all the migrations and pycache files except 'init.py', but nothing works.

I would very much appreciate your help. :)

*The version of my Django is 3.0.4

Upvotes: 1

Views: 796

Answers (1)

lbris
lbris

Reputation: 1239

Django automatically creates a column named ..._id in DATABASE but in your code you still have to use user instead of user_id since you named your field like this user = models.ForeignKey(...)

Try again with main_todolist.user

Upvotes: 1

Related Questions