djangodjango-modelsdjango-viewsprojectdjango-database

Django user_id from django db missing in application models.py


A few days ago I started this tutorial on Django: https://www.youtube.com/watch?v=sm1mokevMWk

The video is about how to set up your first django project, using some libraries from django, as well as bootstrap to style your website. He goes on about how to setup the database, website and everything else you need for the example of a todolist website and at the end of the video series he goes over setting up Users, as well as user specific todo lists. I tried recreating the process and up to the point of the user specific todo lists it was all very understandable and easy to follow. Yet as I tried implementing pretty much the last step of the implementation i get the following error:

OperationalError at /create/
table main_todolist has no column named user_id
Request Method: POST
Request URL:    http://127.0.0.1:8000/create/
Django Version: 5.0.1
Exception Type: OperationalError
Exception Value:    
table main_todolist has no column named user_id
Exception Location: C:\\Entwicklung\\Python\\Python312\\Lib\\site-packages\\django\\db\\backends\\sqlite3\\base.py, line 328, in execute
Raised during:  main.views.create
Python Executable:  C:\\Entwicklung\\Python\\Python312\\python.exe
Python Version: 3.12.0
Python Path:    
\['C:\\Users\\brenneckeo\\Desktop\\Django Projekt\\meineSeite',
'C:\\Entwicklung\\Python\\Python312\\python312.zip',
'C:\\Entwicklung\\Python\\Python312\\DLLs',
'C:\\Entwicklung\\Python\\Python312\\Lib',
'C:\\Entwicklung\\Python\\Python312',
'C:\\Entwicklung\\Python\\Python312\\Lib\\site-packages'\]
Server time:    Wed, 28 Feb 2024 14:18:52 +0000

The Error pops up when I try to create a ToDoList. When you do that you get taken to the /view page where the items (which is a database model, referring to the model of ToDoList) of the TodoList you created are being listed and you can add new ones and check up any of the items. This is how it is supposed to work, yet i guess what I could read out of the error Code was that apparently the todolist had no user_id attribute which should refer to the User, so that you can view different to do lists you have created. In the code of the models.py file I created the guy from the video used the following packages:

from django.db import models
from django.contrib.auth.models import User

Then in the models.py file my Databases are being created like this:

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

    def __str__(self):
        return self.name
    
class Item(models.Model):
    todolist = models.ForeignKey(ToDoList, on_delete=models.CASCADE)
    text = models.CharField(max_length=200)
    complete = models.BooleanField()
    
    def __str__(self):
        return self.text

So judging from the error code I guess I have to add a user_id attribute as foreign key pointing to users or something? The guy in the video didn't get this error message. I'm guessing it's because it is from 3 years ago or something but I don't know exactly.

I thought that since it's searching for some user_id attribute I had to add it to the models but this led to no success. This:

from django.db import models
from django.contrib.auth.models import User

# Models

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

    def __str__(self):
        return self.name
    
class Item(models.Model):
    todolist = models.ForeignKey(ToDoList, on_delete=models.CASCADE)
    text = models.CharField(max_length=200)
    complete = models.BooleanField()
    
    def __str__(self):
        return self.text

...got me this error code when trying to create a todolist:

TypeError: Field.__init__() got an unexpected keyword argument 'on_delete'


Solution

  • Ok, so I got some help from a coworker and the solution was to just delete the sqlite3 database and rerun the migrations to set it all up again. Thanks for the help but my Issue is resolved.