Reputation: 219
I've been solving this problem for the entire day. My code in my models.py is the following:
from django.db import models
from django.contrib.auth.models import User
from users.models import TeacherProfile, StudentProfile
class Course(models.Model):
name = models.CharField(max_length=100)
desc = models.TextField()
short_code = models.CharField(max_length=5)
teacher = models.ForeignKey(TeacherProfile, null=True, on_delete=models.SET_NULL)
students = models.ManyToManyField(StudentProfile)
def __str__(self):
return self.name
class Partition(models.Model):
name = models.CharField(max_length=20)
chunk = models.FloatField()
result = models.FloatField(blank=True)
course = models.ForeignKey(Course, on_delete=models.CASCADE)
def __str__(self):
return self.name
class Activity(models.Model):
title = models.CharField(max_length=100)
content = models.TextField()
file = models.FileField(blank=True)
course = models.ForeignKey(Course, on_delete=models.CASCADE)
parent = models.ForeignKey(Partition, on_delete=models.CASCADE)
def __str__(self):
return self.title
class Grade(models.Model):
grade = models.FloatField()
remarks = models.CharField(max_length=200, blank=True)
activity = models.ForeignKey(Activity, on_delete=models.CASCADE)
student = models.ForeignKey(StudentProfile, on_delete=models.CASCADE)
I've ran the following commands:
python manage.py makemigrations
python manage.py migrate
I even written the following command as per my research to other StackOverFlow questions related to this:
python manage.py migrate --run-syncdb
Only the Course table worked. The Activity and the Grade table received OperationalError - No Such Column and the Partition Table got an OperationalError - No Such Table.
Activity
OperationalError at /admin/course/activity/
no such column: course_activity.parent_id
Request Method: GET
Request URL: http://127.0.0.1:8000/admin/course/activity/
Django Version: 3.1
Exception Type: OperationalError
Exception Value:
no such column: course_activity.parent_id
Exception Location: C:\Users\hp\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\backends\sqlite3\base.py, line 413, in execute
Python Executable: C:\Users\hp\AppData\Local\Programs\Python\Python38-32\python.exe
Python Version: 3.8.5
Python Path:
['C:\\Users\\hp\\Desktop\\TechKnow\\YowBro',
'C:\\Users\\hp\\AppData\\Local\\Programs\\Python\\Python38-32\\python38.zip',
'C:\\Users\\hp\\AppData\\Local\\Programs\\Python\\Python38-32\\DLLs',
'C:\\Users\\hp\\AppData\\Local\\Programs\\Python\\Python38-32\\lib',
'C:\\Users\\hp\\AppData\\Local\\Programs\\Python\\Python38-32',
'C:\\Users\\hp\\AppData\\Local\\Programs\\Python\\Python38-32\\lib\\site-packages']
Server time: Fri, 21 Aug 2020 09:47:26 +0000
Grade
OperationalError at /admin/course/grade/
no such column: course_grade.remarks
Request Method: GET
Request URL: http://127.0.0.1:8000/admin/course/grade/
Django Version: 3.1
Exception Type: OperationalError
Exception Value:
no such column: course_grade.remarks
Exception Location: C:\Users\hp\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\backends\sqlite3\base.py, line 413, in execute
Python Executable: C:\Users\hp\AppData\Local\Programs\Python\Python38-32\python.exe
Python Version: 3.8.5
Python Path:
['C:\\Users\\hp\\Desktop\\TechKnow\\YowBro',
'C:\\Users\\hp\\AppData\\Local\\Programs\\Python\\Python38-32\\python38.zip',
'C:\\Users\\hp\\AppData\\Local\\Programs\\Python\\Python38-32\\DLLs',
'C:\\Users\\hp\\AppData\\Local\\Programs\\Python\\Python38-32\\lib',
'C:\\Users\\hp\\AppData\\Local\\Programs\\Python\\Python38-32',
'C:\\Users\\hp\\AppData\\Local\\Programs\\Python\\Python38-32\\lib\\site-packages']
Server time: Fri, 21 Aug 2020 09:47:28 +0000
Partition
OperationalError at /admin/course/partition/
no such table: course_partition
Request Method: GET
Request URL: http://127.0.0.1:8000/admin/course/partition/
Django Version: 3.1
Exception Type: OperationalError
Exception Value:
no such table: course_partition
Exception Location: C:\Users\hp\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\backends\sqlite3\base.py, line 413, in execute
Python Executable: C:\Users\hp\AppData\Local\Programs\Python\Python38-32\python.exe
Python Version: 3.8.5
Python Path:
['C:\\Users\\hp\\Desktop\\TechKnow\\YowBro',
'C:\\Users\\hp\\AppData\\Local\\Programs\\Python\\Python38-32\\python38.zip',
'C:\\Users\\hp\\AppData\\Local\\Programs\\Python\\Python38-32\\DLLs',
'C:\\Users\\hp\\AppData\\Local\\Programs\\Python\\Python38-32\\lib',
'C:\\Users\\hp\\AppData\\Local\\Programs\\Python\\Python38-32',
'C:\\Users\\hp\\AppData\\Local\\Programs\\Python\\Python38-32\\lib\\site-packages']
Server time: Fri, 21 Aug 2020 09:47:29 +0000
What did I do wrong?
UPDATE: I've tried putting default values to the tables. Sadly, it didn't work. Help please.
UPDATE #2: I ran python manage.py sqlmigrate course 0001
. Contrary to the error it shows in the actual site, the returned SQL commands are shown below (I highlighted the SQL command for creating the table Partition for emphasis):
ASSUMPTION: Maybe the error is in the migration
command. I manually deleted the tables from my DB Browser for SQLite. Then, I tried running the makemigrations
and the migrations
command. Though it migrates a SQL code, it doesn't create anything at all. How can we fix this?
I SOLVED THE PROBLEM! Check my answer below.
Upvotes: 1
Views: 3494
Reputation: 219
I FIXED THE PROBLEM.
Thanks to kerasbaz, I checked my database configuration in my Django settings. I thought that I should comment the configuration out for the SQLite and insert the PostgreSQL.
So what I first did is to create a new database through my pgAdmin. Now, you need to have pgAdmin and psycopg2 installed (pip install psycopg2
). Next, I edited my database settings in my project's settings.
Here's a sample from the Django documentation:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'mydatabase',
'USER': 'mydatabaseuser',
'PASSWORD': 'mypassword',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
Finally, I ran makemigrations
and migrate
commands. Voila! I solved the problem I've been solving for five hours.
Upvotes: 1
Reputation: 131
You can try to put related name in your models eg
class Activity(models.Model):
title = models.CharField(max_length=100)
content = models.TextField()
file = models.FileField(blank=True)
course = models.ForeignKey(Course,related_name='course_id' on_delete=models.CASCADE)
parent = models.ForeignKey(Partition,related_name='parent_id' on_delete=models.CASCADE)
def __str__(self):
return self.title
Upvotes: 1