Django shell not able to execute ORM query - python

I am using Django shell for debug purpose of my project by using command python manage.py shell.
I have found that I am not able to execute ORM queries from shell & getting below kind of error.
from base.models import Template_Form, Template_Form_Field
Template_Form_Field.objects.all()
After executing above code in shell I am getting below error.
Traceback (most recent call last):
File "D:\production\siralo\siralo_py3\venv\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
psycopg2.errors.UndefinedTable: relation "base_template_form_field" does not exist
LINE 1: ...ions", "base_template_form_field"."sequence" FROM "base_temp...
^
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "D:\production\siralo\siralo_py3\venv\lib\site-packages\django\db\models\query.py", line 250, in __repr__
data = list(self[:REPR_OUTPUT_SIZE + 1])
File "D:\production\siralo\siralo_py3\venv\lib\site-packages\django\db\models\query.py", line 274, in __iter__
self._fetch_all()
File "D:\production\siralo\siralo_py3\venv\lib\site-packages\django\db\models\query.py", line 1242, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "D:\production\siralo\siralo_py3\venv\lib\site-packages\django\db\models\query.py", line 55, in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
File "D:\production\siralo\siralo_py3\venv\lib\site-packages\django\db\models\sql\compiler.py", line 1100, in execute_sql
cursor.execute(sql, params)
File "D:\production\siralo\siralo_py3\venv\lib\site-packages\django\db\backends\utils.py", line 99, in execute
return super().execute(sql, params)
File "D:\production\siralo\siralo_py3\venv\lib\site-packages\django\db\backends\utils.py", line 67, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "D:\production\siralo\siralo_py3\venv\lib\site-packages\django\db\backends\utils.py", line 76, in _execute_with_wrappers
return executor(sql, params, many, context)
File "D:\production\siralo\siralo_py3\venv\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "D:\production\siralo\siralo_py3\venv\lib\site-packages\django\db\utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "D:\production\siralo\siralo_py3\venv\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "base_template_form_field" does not exist
LINE 1: ...ions", "base_template_form_field"."sequence" FROM "base_temp...
Does anybody have idea why am I getting above error & how to get rid of it.
I have tried to search on google but not able to find any solution.
Template form field model is as below.
class Template_Form_Field(models.Model):
template_form = models.ForeignKey(Template_Form, on_delete=models.CASCADE)
name = models.CharField(max_length=40, verbose_name='Name')
caption = models.CharField(max_length=80, verbose_name='Caption')
value_options = models.TextField(null=True, blank=True, verbose_name='Value options')
sequence = models.IntegerField(blank=True, null=True, verbose_name='Sequence')
Thanks.

just run python manage.py migrate
this error is because you have just run python manage.py makemigrations but did'nt run python manage.py migrate.
i also encounter the same error in shell while i have not run the migrate command below is the screen shot of error i get this is the screenshot of the same error

Related

Django tests suddenly failing for no apparent reason

I have a django project with hundreds of unit tests and after a big update that added about 50 more tests, all the project's tests that creates an instance of a specific model are failing with the following error message:
Traceback (most recent call last):
File ".../venv/lib/python3.9/site-packages/django/db/models/sql/compiler.py", line 1142, in execute_sql
cursor.execute(sql, params)
File ".../venv/lib/python3.9/site-packages/django/db/backends/utils.py", line 67, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File ".../venv/lib/python3.9/site-packages/django/db/backends/utils.py", line 76, in _execute_with_wrappers
return executor(sql, params, many, context)
File ".../venv/lib/python3.9/site-packages/django/db/backends/utils.py", line 79, in _execute
self.db.validate_no_broken_transaction()
File ".../venv/lib/python3.9/site-packages/django/db/backends/base/base.py", line 437, in validate_no_broken_transaction
raise TransactionManagementError(
django.db.transaction.TransactionManagementError: An error occurred in the current transaction. You can't execute queries until the end of the 'atomic' block.
During handling of the above exception, another exception occurred:
File ".../venv/lib/python3.9/site-packages/django/db/models/manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File ".../venv/lib/python3.9/site-packages/django/db/models/query.py", line 653, in first
for obj in (self if self.ordered else self.order_by('pk'))[:1]:
File ".../venv/lib/python3.9/site-packages/django/db/models/query.py", line 274, in __iter__
self._fetch_all()
File ".../venv/lib/python3.9/site-packages/django/db/models/query.py", line 1242, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File ".../venv/lib/python3.9/site-packages/django/db/models/query.py", line 55, in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
File ".../venv/lib/python3.9/site-packages/django/db/models/sql/compiler.py", line 1145, in execute_sql
cursor.close()
File ".../venv/lib/python3.9/site-packages/MySQLdb/cursors.py", line 83, in close
while self.nextset():
File ".../venv/lib/python3.9/site-packages/MySQLdb/cursors.py", line 137, in nextset
nr = db.next_result()
MySQLdb._exceptions.OperationalError: (2006, '')
The update did not change any models at all and no migrations were made.
The tests seem to work if I change class SomeTest(TestCase) to class SomeTest(TransactionTestCase) in all tests, but then they get extremely slow and take several hours to run. (They usually took 2 minutes two run before this error.)
I'm using python 3.9, django 2.2.28, and mysql 8.0.23.
I tried upgrading to django 3.2 but that did not fix the problem.
I also tried not running all the new tests with --exclude-tag on the command line, but it would still raise the same errors.

Django tutorial part2 went wrong

I'm following the tutorial and I received a traceback ~>
Question.objects.all()Traceback (most recent call last): File "/home/pingounica/.virtualenvs/django3/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute return self.cursor.execute(sql, params) File "/home/pingounica/.virtualenvs/django3/lib/python3.9/site-packages/django/db/backends/sqlite3/base.py", line 423, in execute return Database.Cursor.execute(self, query, params) sqlite3.OperationalError: no such table: polls_question The above exception was the direct cause of the following exception: Traceback (most recent call last): File "<console>", line 1, in <module> File "/home/pingounica/.virtualenvs/django3/lib/python3.9/site-packages/django/db/models/query.py", line 256, in __repr__ data = list(self[:REPR_OUTPUT_SIZE + 1]) File "/home/pingounica/.virtualenvs/django3/lib/python3.9/site-packages/django/db/models/query.py", line 262, in __len__ self._fetch_all() File "/home/pingounica/.virtualenvs/django3/lib/python3.9/site-packages/django/db/models/query.py", line 1324, in _fetch_all self._result_cache = list(self._iterable_class(self)) File "/home/pingounica/.virtualenvs/django3/lib/python3.9/site-packages/django/db/models/query.py", line 51, in __iter__ results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size) File "/home/pingounica/.virtualenvs/django3/lib/python3.9/site-packages/django/db/models/sql/compiler.py", line 1175, in execute_sql cursor.execute(sql, params) File "/home/pingounica/.virtualenvs/django3/lib/python3.9/site-packages/django/db/backends/utils.py", line 98, in execute return super().execute(sql, params) File "/home/pingounica/.virtualenvs/django3/lib/python3.9/site-packages/django/db/backends/utils.py", line 66, in execute return self._execute_with_wrappers(sql, params, many=False, executor=self._execute) File "/home/pingounica/.virtualenvs/django3/lib/python3.9/site-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers return executor(sql, params, many, context) File "/home/pingounica/.virtualenvs/django3/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute return self.cursor.execute(sql, params) File "/home/pingounica/.virtualenvs/django3/lib/python3.9/site-packages/django/db/utils.py", line 90, in __exit__ raise dj_exc_value.with_traceback(traceback) from exc_value File "/home/pingounica/.virtualenvs/django3/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute return self.cursor.execute(sql, params) File "/home/pingounica/.virtualenvs/django3/lib/python3.9/site-packages/django/db/backends/sqlite3/base.py", line 423, in execute return Database.Cursor.execute(self, query, params) django.db.utils.OperationalError: no such table: polls_question >>> from django.utils import timezone >>> q = Question(question_text="What's new?", pub_date=timezone.now()) >>> q.save() Traceback (most recent call last): File "/home/pingounica/.virtualenvs/django3/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute return self.cursor.execute(sql, params) File "/home/pingounica/.virtualenvs/django3/lib/python3.9/site-packages/django/db/backends/sqlite3/base.py", line 423, in execute return Database.Cursor.execute(self, query, params) sqlite3.OperationalError: no such table: polls_question The above exception was the direct cause of the following exception: Traceback (most recent call last): File "<console>", line 1, in <module> File "/home/pingounica/.virtualenvs/django3/lib/python3.9/site-packages/django/db/models/base.py", line 726, in save self.save_base(using=using, force_insert=force_insert, File "/home/pingounica/.virtualenvs/django3/lib/python3.9/site-packages/django/db/models/base.py", line 763, in save_base updated = self._save_table(b/backends/utils.py", line 84, in _execute File "/home/pingounica/.virtualenvs/django3/lib/python3.9/site-packages/django/db/models/base.py", line 868, in _save_table results = self._do_insert(cls._base_manager, using, fields, returning_fields, raw) File "/home/pingounica/.virtualenvs/django3/lib/python3.9/site-packages/django/db/models/base.py", line 906, in _do_insert return manager._insert( File "/home/pingounica/.virtualenvs/django3/lib/python3.9/site-packages/django/db/models/manager.py", line 85, in manager_method return getattr(self.get_queryset(), name)(*args, **kwargs) File "/home/pingounica/.virtualenvs/django3/lib/python3.9/site-packages/django/db/models/query.py", line 1270, in _insert return query.get_compiler(using=using).execute_sql(returning_fields) File "/home/pingounica/.virtualenvs/django3/lib/python3.9/site-packages/django/db/models/sql/compiler.py", line 1416, in execute_sql cursor.execute(sql, params) File "/home/pingounica/.virtualenvs/django3/lib/python3.9/site-packages/django/db/backends/utils.py", line 98, in execute return super().execute(sql, params) File "/home/pingounica/.virtualenvs/django3/lib/python3.9/site-packages/django/db/backends/utils.py", line 66, in execute return self._execute_with_wrappers(sql, params, many=False, executor=self._execute) File "/home/pingounica/.virtualenvs/django3/lib/python3.9/site-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers return executor(sql, params, many, context) File "/home/pingounica/.virtualenvs/django3/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute return self.cursor.execute(sql, params) File "/home/pingounica/.virtualenvs/django3/lib/python3.9/site-packages/django/db/utils.py", line 90, in __exit__ raise dj_exc_value.with_traceback(traceback) from exc_value File "/home/pingounica/.virtualenvs/django3/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute return self.cursor.execute(sql, params) File "/home/pingounica/.virtualenvs/django3/lib/python3.9/site-packages/django/db/backends/sqlite3/base.py", line 423, in execute return Database.Cursor.execute(self, query, params)django.db.utils.OperationalError: no such table: polls_question
I did the makemigrations and migrate, nothing changed or helped.
After I included at db.sqlite3 by hand the tables and that gave me another traceback ~>
Question.objects.all()Traceback (most recent call last): File "/home/pingounica/.virtualenvs/django3/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute return self.cursor.execute(sql, params) File "/home/pingounica/.virtualenvs/django3/lib/python3.9/site-packages/django/db/backends/sqlite3/base.py", line 423, in execute return Database.Cursor.execute(self, query, params)sqlite3.OperationalError: no such column: polls_question.question_textThe above exception was the direct cause of the following exception:Traceback (most recent call last): File "<console>", line 1, in <module> File "/home/pingounica/.virtualenvs/django3/lib/python3.9/site-packages/django/db/models/query.py", line 256, in __repr__ data = list(self[:REPR_OUTPUT_SIZE + 1]) File "/home/pingounica/.virtualenvs/django3/lib/python3.9/site-packages/django/db/models/query.py", line 262, in __len__ self._fetch_all() File "/home/pingounica/.virtualenvs/django3/lib/python3.9/site-packages/django/db/models/query.py", line 1324, in _fetch_all self._result_cache = list(self._iterable_class(self)) File "/home/pingounica/.virtualenvs/django3/lib/python3.9/site-packages/django/db/models/query.py", line 51, in __iter__ results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size) File "/home/pingounica/.virtualenvs/django3/lib/python3.9/site-packages/django/db/models/sql/compiler.py", line 1175, in execute_sql cursor.execute(sql, params) File "/home/pingounica/.virtualenvs/django3/lib/python3.9/site-packages/django/db/backends/utils.py", line 98, in execute return super().execute(sql, params) File "/home/pingounica/.virtualenvs/django3/lib/python3.9/site-packages/django/db/backends/utils.py", line 66, in execute return self._execute_with_wrappers(sql, params, many=False, executor=self._execute) File "/home/pingounica/.virtualenvs/django3/lib/python3.9/site-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers return executor(sql, params, many, context) File "/home/pingounica/.virtualenvs/django3/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute return self.cursor.execute(sql, params) File "/home/pingounica/.virtualenvs/django3/lib/python3.9/site-packages/django/db/utils.py", line 90, in __exit__ raise dj_exc_value.with_traceback(traceback) from exc_value File "/home/pingounica/.virtualenvs/django3/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute return self.cursor.execute(sql, params) File "/home/pingounica/.virtualenvs/django3/lib/python3.9/site-packages/django/db/backends/sqlite3/base.py", line 423, in execute return Database.Cursor.execute(self, query, params) django.db.utils.OperationalError: no such column: polls_question.question_text >>> Question.objects.all() Traceback (most recent call last): File "/home/pingounica/.virtualenvs/django3/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute return self.cursor.execute(sql, params) File "/home/pingounica/.virtualenvs/django3/lib/python3.9/site-packages/django/db/backends/sqlite3/base.py", line 423, in execute return Database.Cursor.execute(self, query, params) sqlite3.OperationalError: no such column: polls_question.question_text The above exception was the direct cause of the following exception: Traceback (most recent call last): File "<console>", line 1, in <module> File "/home/pingounica/.virtualenvs/django3/lib/python3.9/site-packages/django/db/models/query.py", line 256, in __repr__ data = list(self[:REPR_OUTPUT_SIZE + 1]) File "/home/pingounica/.virtualenvs/django3/lib/python3.9/site-packages/django/db/models/query.py", line 262, in __len__ self._fetch_all() File "/home/pingounica/.virtualenvs/django3/lib/python3.9/site-packages/django/db/models/query.py", line 1324, in _fetch_all self._result_cache = list(self._iterable_class(self)) File "/home/pingounica/.virtualenvs/django3/lib/python3.9/site-packages/django/db/models/query.py", line 51, in __iter__ results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size) File "/home/pingounica/.virtualenvs/django3/lib/python3.9/site-packages/django/db/models/sql/compiler.py", line 1175, in execute_sql cursor.execute(sql, params) File "/home/pingounica/.virtualenvs/django3/lib/python3.9/site-packages/django/db/backends/utils.py", line 98, in execute return super().execute(sql, params) File "/home/pingounica/.virtualenvs/django3/lib/python3.9/site-packages/django/db/backends/utils.py", line 66, in execute return self._execute_with_wrappers(sql, params, many=False, executor=self._execute) File "/home/pingounica/.virtualenvs/django3/lib/python3.9/site-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers return executor(sql, params, many, context) File "/home/pingounica/.virtualenvs/django3/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute return self.cursor.execute(sql, params) File "/home/pingounica/.virtualenvs/django3/lib/python3.9/site-packages/django/db/utils.py", line 90, in __exit__ raise dj_exc_value.with_traceback(traceback) from exc_value File "/home/pingounica/.virtualenvs/django3/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute return self.cursor.execute(sql, params) File "/home/pingounica/.virtualenvs/django3/lib/python3.9/site-packages/django/db/backends/sqlite3/base.py", line 423, in execute return Database.Cursor.execute(self, query, params)django.db.utils.OperationalError: no such column: polls_question.question_text
that's my models.py ~>
from django.db import models
class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
def __str__(self):
return self.question_text
class Choice(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
def __str__(self):
return self.choice_text
and my manage.py ~>
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys
def main():
"""Run administrative tasks."""
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)
if __name__ == '__main__':
main()
What am I doing wrong?
Seems like you created your app using the command manage.py startapp polls but I see mysite in your manage.py
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')
Django creates the table names in the database using a combination of the app name and the model name. In your case the app name is supposed to be polls I believe?, but for some reason the app name in your manage.py is mysite.
I suggest you start from scratch and try again with the manage.py startapp <app name> command, be careful of changing any references to the actual app name manually in any of the generated files.

Django Shell Default Database

I have an app named steve_charts with a models.py containing the class Atmospheric:
class Atmospheric(models.Model):
time = models.DateTimeField(unique=True)
temperature = models.IntegerField(blank=True, null=True)
humidity = models.IntegerField(blank=True, null=True)
kpa = models.IntegerField(blank=True, null=True)
class Meta:
managed = False
db_table = 'atmospheric'
This refers to a legacy DB I am accessing. In settings.py my postgresql DB is correctly defined:
'steveDB':{
'ENGINE': 'django.db.backends.postgresql',
'HOST': 'localhost',
'NAME': 'sensor_data',
'USER': 'pi'
},
'default': { ## NOT EVEN USING THIS THING
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
I ran python manage.py inspectdb --database steveDB > models.py to get me a fresh new models.py no problems so far...
I ran python manage.py makemigrations steve_charts (which has been added to the installed apps section) and everything went smoothly. Django accessed my DB, inspected the table, made a new models.py for me no problem. Applied migrations no problem.
So now I would like to check the DB in the shell:
python manage.py shell
>>>from steve_charts.models import Atmospheric No problems.
>>>Atmospheric.objects.all() This should dump all the rows in the table, right? Instead I get django.db.utils.OperationalError: no such table: atmospheric
Full stack trace:
>>> Atmospheric.objects.all()
Traceback (most recent call last):
File "/home/pi/steve/env/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/home/pi/steve/env/lib/python3.7/site-packages/django/db/backends/sqlite3/base.py", line 423, in execute
return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: no such table: atmospheric
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/pi/steve/env/lib/python3.7/site-packages/django/db/models/query.py", line 256, in __repr__
data = list(self[:REPR_OUTPUT_SIZE + 1])
File "/home/pi/steve/env/lib/python3.7/site-packages/django/db/models/query.py", line 280, in __iter__
self._fetch_all()
File "/home/pi/steve/env/lib/python3.7/site-packages/django/db/models/query.py", line 1324, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "/home/pi/steve/env/lib/python3.7/site-packages/django/db/models/query.py", line 51, in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
File "/home/pi/steve/env/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 1175, in execute_sql
cursor.execute(sql, params)
File "/home/pi/steve/env/lib/python3.7/site-packages/django/db/backends/utils.py", line 98, in execute
return super().execute(sql, params)
File "/home/pi/steve/env/lib/python3.7/site-packages/django/db/backends/utils.py", line 66, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/home/pi/steve/env/lib/python3.7/site-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/home/pi/steve/env/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/home/pi/steve/env/lib/python3.7/site-packages/django/db/utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/home/pi/steve/env/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/home/pi/steve/env/lib/python3.7/site-packages/django/db/backends/sqlite3/base.py", line 423, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such table: atmospheric
>>> django.db.utils.OperationalError: no such table: atmospheric
File "<console>", line 1
django.db.utils.OperationalError: no such table: atmospheric
^
SyntaxError: invalid syntax
I'm using a RPi 3 model B. Raspberry Pi OS if that makes any difference.
Seems to me that the shell is only using the default DB as per trace. Is this expected behavior or am I being a stupid fella here?
You can use using() method in ORM query to specify database to use
Like
Model.objects.using('steveDB').all()

django.db.utils.OperationalError: no such table:

I recently started a django project and I am having trouble accessing a sqlite3 database that I recently added. I ran python manage.py inspectdb --database 'football' after inserting the new database into settings.py. This gave me the file that I copy pasted into models.py. From there I ran makemigrations which gave me this error:
SystemCheckError: System check identified some issues:
ERRORS:
teambuilder.Countries.country_id: (fields.E007) Primary keys must not have null=True.
HINT: Set null=False on the field, or remove primary_key=True argument.
teambuilder.Playertransferhistory: (models.E004) 'id' can only be used as a field name if the field also sets 'primary_key=True'.
teambuilder.Teams.team_id: (fields.E007) Primary keys must not have null=True.
HINT: Set null=False on the field, or remove primary_key=True argument.
(venv) Jacks-MBP:SportWebsite Tilley$ python manage.py makemigrations
SystemCheckError: System check identified some issues:
ERRORS:
teambuilder.Playertransferhistory.id: (fields.E007) Primary keys must not have null=True.
HINT: Set null=False on the field, or remove primary_key=True argument.
From there I changed null to false where needed and reran makemigrations
which gave me this:
Migrations for 'teambuilder':
teambuilder/migrations/0001_initial.py
- Create model Countries
- Create model LeagueGames
- Create model Leagues
- Create model Playerinfo
- Create model Playertransferhistory
- Create model Teams
followed by migrate which outputted:
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions, sites, teambuilder
Running migrations:
No migrations to apply.
I then ran python manage.py shell and typed in the following commands:
from teambuilder.models import *
Leagues.objects.all()
which presented me with this red wall:
Traceback (most recent call last):
File "/Users/Tilley/PycharmProjects/SportWeb/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "/Users/Tilley/PycharmProjects/SportWeb/venv/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py", line 298, in execute
return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: no such table: Leagues
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/Users/Tilley/PycharmProjects/SportWeb/venv/lib/python3.6/site-packages/django/db/models/query.py", line 244, in __repr__
data = list(self[:REPR_OUTPUT_SIZE + 1])
File "/Users/Tilley/PycharmProjects/SportWeb/venv/lib/python3.6/site-packages/django/db/models/query.py", line 268, in __iter__
self._fetch_all()
File "/Users/Tilley/PycharmProjects/SportWeb/venv/lib/python3.6/site-packages/django/db/models/query.py", line 1186, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "/Users/Tilley/PycharmProjects/SportWeb/venv/lib/python3.6/site-packages/django/db/models/query.py", line 54, in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
File "/Users/Tilley/PycharmProjects/SportWeb/venv/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1065, in execute_sql
cursor.execute(sql, params)
File "/Users/Tilley/PycharmProjects/SportWeb/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 100, in execute
return super().execute(sql, params)
File "/Users/Tilley/PycharmProjects/SportWeb/venv/lib/python3.6/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/Tilley/PycharmProjects/SportWeb/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/Users/Tilley/PycharmProjects/SportWeb/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "/Users/Tilley/PycharmProjects/SportWeb/venv/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/Users/Tilley/PycharmProjects/SportWeb/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "/Users/Tilley/PycharmProjects/SportWeb/venv/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py", line 298, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such table: Leagues
I know that the table does exist but I cannot understand why I'm getting this error. I've seen similar questions here on SO but none of the solutions have fixed my problem. Any help would be greatly appreciated.

Django - can't run makemigrations: "no such table" even after running reset_db

I am unable to run makemigrations, migrate, or anything else (flush, reset_db from django-extensions) if I have a certain app in my INSTALLED_APPS.
The app is called issues and has one model:
class Issue(models.Model):
title = models.CharField(max_length=255)
description = models.CharField(max_length=1000)
sent = models.BooleanField()
And it was working before (makemigrations and migrate ran fine and I could use the app/model correctly), until I tried adding:
severity = models.IntegerField()
and tried to run makemigrations. I don't have the error or remember it anymore, but since then everything is broken, even after removing severity from the model.
Everything works if I remove the issues app from my settings.py.
The error I get:
madjura#madjura-E6228:~/workspace/budget/src$ python3.5 manage.py makemigrationsTraceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/usr/local/lib/python3.5/dist-packages/django/db/backends/sqlite3/base.py", line 337, in execute
return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: no such table: issues_issue
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python3.5/dist-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python3.5/dist-packages/django/core/management/__init__.py", line 341, in execute
django.setup()
File "/usr/local/lib/python3.5/dist-packages/django/__init__.py", line 27, in setup
apps.populate(settings.INSTALLED_APPS)
File "/usr/local/lib/python3.5/dist-packages/django/apps/registry.py", line 115, in populate
app_config.ready()
File "/home/madjura/workspace/budget/src/issues/apps.py", line 16, in ready
issues.models.Issue.objects.check_and_send_unsent_issues()
File "/home/madjura/workspace/budget/src/issues/models.py", line 18, in check_and_send_unsent_issues
for issue in issues:
File "/usr/local/lib/python3.5/dist-packages/django/db/models/query.py", line 256, in __iter__
self._fetch_all()
File "/usr/local/lib/python3.5/dist-packages/django/db/models/query.py", line 1087, in _fetch_all
self._result_cache = list(self.iterator())
File "/usr/local/lib/python3.5/dist-packages/django/db/models/query.py", line 54, in __iter__
results = compiler.execute_sql()
File "/usr/local/lib/python3.5/dist-packages/django/db/models/sql/compiler.py", line 835, in execute_sql
cursor.execute(sql, params)
File "/usr/local/lib/python3.5/dist-packages/django/db/backends/utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/usr/local/lib/python3.5/dist-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/usr/local/lib/python3.5/dist-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/usr/local/lib/python3.5/dist-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/usr/local/lib/python3.5/dist-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/usr/local/lib/python3.5/dist-packages/django/db/backends/sqlite3/base.py", line 337, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such table: issues_issue
While issues is in INSTALLED_APPS I get the same error when running migrate, flush and reset_db.
I have tried running flush and reset_db with issues removed from INSTALLED_APPS, this did not solve the problem.
I have tried doing the above and then running makemigrations and migrate, this does also not work. As soon as I put issues back in INSTALLED_APPS everything is broken.
How can I fix this?
EDIT:
Maybe relevant, the issue model has a Manager with a function:
class IssueManager(models.Manager):
"""Manager for the Issue class."""
def check_and_send_unsent_issues(self):
"""
Checks for unsent Issue objects (Issue.sent = False) and attempts
to send them.
Issues that have been sent are deleted.
If the issue fails to be sent for whatever reason, it is not deleted.
Does nothing if there are no unsent issues.
This method is called once when the server starts.
"""
issues = self.get_queryset().filter(sent=False)
for issue in issues:
try:
make_issue(issue.title, issue.description)
issue.delete()
except PostIssueException:
pass
Using apps.py I check for unsent issues and post them on Gitlab.
EDIT 2:
Issue resolved by commenting the line below which appears in my apps.py, in ready():
issues.models.Issue.objects.check_and_send_unsent_issues()
Which somehow caused things to break, I don't understand why. Could someone explain that please?
Try moving the line import issues.models into def ready() to prevent loading the models too early.

Categories