hello after running this code in a models.py file:
from django.db import models # Create your models here. class Topic(models.Model): top_name = models.CharField(max_length=264,unique=True) def __str__(self): return self.top_name class Webpage(models.Model): topic = models.ForeignKey( 'Topic', on_delete=models.CASCADE, ) name = models.CharField(max_length=264,unique=True) url = models.URLField(unique=True) def __str__(self): return self.name class AccessRecord(models.Model): name = models.ForeignKey( 'Webpage', on_delete=models.CASCADE, ) date = models.DateField() def __str__(self): return str(self.date)
As a note, I was having trouble with migrations as I kept getting error messages related to the .ForeignKey so I got the following code from some other forum, and it finally ran the migrations but I’m not sure if this code is the problem…
topic = models.ForeignKey( 'Topic', on_delete=models.CASCADE, )
After successful migrations, I ran
python manage.py migrate
everything seemed to migrate fine. Then I ran
python manage.py shell
then I ran:
>>> from first_app.models import Topic >>> print(Topic.objects.all())
but got the following error:
OperationalError: no such table: first_app_topic
here’s the full read out:
Traceback (most recent call last): File "/Users/homepage/opt/anaconda3/envs/MyDjangoEnv/lib/python3.8/site-packages/django/db/backends/utils .py", line 86, in _execute return self.cursor.execute(sql, params) File "/Users/homepage/opt/anaconda3/envs/MyDjangoEnv/lib/python3.8/site-packages/django/db/backends/sqlit e3/base.py", line 396, in execute return Database.Cursor.execute(self, query, params) sqlite3.OperationalError: no such table: first_app_topic The above exception was the direct cause of the following exception: Traceback (most recent call last): File "<console>", line 1, in <module> File "/Users/homepage/opt/anaconda3/envs/MyDjangoEnv/lib/python3.8/site-packages/django/db/models/query.p y", line 252, in __repr__ data = list(self[:REPR_OUTPUT_SIZE + 1]) File "/Users/homepage/opt/anaconda3/envs/MyDjangoEnv/lib/python3.8/site-packages/django/db/models/query.p y", line 258, in __len__ self._fetch_all() File "/Users/homepage/opt/anaconda3/envs/MyDjangoEnv/lib/python3.8/site-packages/django/db/models/query.p y", line 1261, in _fetch_all self._result_cache = list(self._iterable_class(self)) File "/Users/homepage/opt/anaconda3/envs/MyDjangoEnv/lib/python3.8/site-packages/django/db/models/query.p y", line 57, in __iter__ results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size) File "/Users/homepage/opt/anaconda3/envs/MyDjangoEnv/lib/python3.8/site-packages/django/db/models/sql/com piler.py", line 1144, in execute_sql cursor.execute(sql, params) File "/Users/homepage/opt/anaconda3/envs/MyDjangoEnv/lib/python3.8/site-packages/django/db/backends/utils .py", line 100, in execute return super().execute(sql, params) File "/Users/homepage/opt/anaconda3/envs/MyDjangoEnv/lib/python3.8/site-packages/django/db/backends/utils .py", line 68, in execute return self._execute_with_wrappers(sql, params, many=False, executor=self._execute) File "/Users/homepage/opt/anaconda3/envs/MyDjangoEnv/lib/python3.8/site-packages/django/db/backends/utils .py", line 77, in _execute_with_wrappers return executor(sql, params, many, context) File "/Users/homepage/opt/anaconda3/envs/MyDjangoEnv/lib/python3.8/site-packages/django/db/backends/utils .py", line 86, in _execute return self.cursor.execute(sql, params) File "/Users/homepage/opt/anaconda3/envs/MyDjangoEnv/lib/python3.8/site-packages/django/db/utils.py", lin e 90, in __exit__ raise dj_exc_value.with_traceback(traceback) from exc_value File "/Users/homepage/opt/anaconda3/envs/MyDjangoEnv/lib/python3.8/site-packages/django/db/backends/utils .py", line 86, in _execute return self.cursor.execute(sql, params) File "/Users/homepage/opt/anaconda3/envs/MyDjangoEnv/lib/python3.8/site-packages/django/db/backends/sqlit e3/base.py", line 396, in execute return Database.Cursor.execute(self, query, params) django.db.utils.OperationalError: no such table: first_app_topic
models.py is in:
My_Django_Stuff/first_project/first_app/migrations/models.py
any help would be greatly appreciated, I’ve read a bunch of other threads on this topic and some say to toss the db.sqlite3 and pycache – is this what I should do?
thnx
Advertisement
Answer
I believe that I fixed the problem by doing the following steps below:
python manage.py migrate python manage.py makemigrations first_app python manage.py migrate python manage.py shell from first_app.models import Topic print(Topic.objects.all())
I believe that b4 I didn’t make the migrations apply to my first_app (the 2nd step) and then run the migration again(the 3rd step). Also I reinstalled django, this time with the pip installer and it seems to be working much better- as I was getting error messages b4. Right after I would install django in the app folder I was working in, it would say that it it can’t find django. This happened off and on. B4 I installed django with the command:
conda install django
but have been having problems with conda, I have to always paste:
PATH=/Users/aleong/opt/anaconda3/bin:$PATH
b4 I create a virtual env with:
conda create --name myDjangoEnv django
Also fixing the code in my models.py file as suggested by Somto (thnx Somto) helped, I replaced my code with:
topic = models.ForeignKey(Topic, on_delete= models.CASCADE)