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

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.

Related

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 shell not able to execute ORM query

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

Unable to migrate models from django to sql server

I'm trying to learn django and have to use sql server as a data base and unable to migrate models using manage.py migrate
I'm using django 2.1.8
Pyodbc
python 3.7.3
I've tried to reinstall django, python and even sql server but it didn't solve the problem.
On running py manage.py migrate, i get the following error
py manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions, testApp
Running migrations:
Applying contenttypes.0001_initial...Traceback (most recent call last):
File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sql_server\pyodbc\base.py", line 546, in execute
return self.cursor.execute(sql, params)
pyodbc.ProgrammingError: ('42S02', '[42S02] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Cannot find the object "django_content_type" because it does not exist or you do not have permissions. (4902) (SQLExecDirectW)')
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
utility.execute()
File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\base.py", line 316, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\base.py", line 353, in execute
output = self.handle(*args, **options)
File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\commands\migrate.py", line 203, in handle
fake_initial=fake_initial,
File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\migrations\executor.py", line 117, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\migrations\executor.py", line 147, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\migrations\executor.py", line 244, in apply_migration
state = migration.apply(state, schema_editor)
File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\migrations\migration.py", line 124, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\migrations\operations\models.py", line 514, in database_forwards
getattr(new_model._meta, self.option_name, set()),
File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\base\schema.py", line 360, in alter_unique_together
self.execute(self._create_unique_sql(model, columns))
File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sql_server\pyodbc\schema.py", line 653, in execute
cursor.execute(sql, params)
File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\utils.py", line 100, in execute
return super().execute(sql, params)
File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\utils.py", line 68, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sql_server\pyodbc\base.py", line 546, in execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: ('42S02', '[42S02] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Cannot find the object "django_content_type" because it does not exist or you do not have permissions. (4902) (SQLExecDirectW)')
I expect it to migrate but it gives me this error.
Might be a different problem, but I was having the same error. In my case I had run python manage.py migrate --fake (per other SO threads) to fix an error I had earlier and django made a record of the migration without actually creating the table. When I ran it again it assumed I was modifying a table that wasn't there.
I eventually solved this by:
Manually creating the table in SQL server with all of the correct fields
Commenting out the model code from everywhere in the models.py
python manage.py makemigrations to remove the fake table from registry
python manage.py migrate to delete the manual table I just made and update the migrations registry
then re-add the table to my models.py
then re-run python manage.py makemigrations
python manage.py migrate to sync django with SQL server

Django Make Migration Error No Such Column

I have a pre-existing model that I am now trying to add an additional field too. It wasn't me that made the original field but I have been adding fields to other classes and making migrations fine.
I want to add an additional image field to the class Event, there is already an image field on this class so I know it can handle image fields, I have tried changing the name around to see if that was a issue too. Here is the field I want to add:
social_media_image = models.ImageField(null=True, blank=True, help_text='Hello World')
This is the error I get when i'm trying to make my migration after adding that code to the model:
django.db.utils.OperationalError: no such column: posts_event.social_media_image
From my understanding of how migrations work there shouldn't be a column called this yet as I haven't made the migration yet that will add it to the DB.
I have tried adding a default value to the field as well but with no luck. I have also tried completely removing the DB along with migration files and trying to recreate them.
Here are the rest of the fields in the model:
slug = AutoSlugField(max_length=50, unique=True, populate_from='title')
content = models.TextField(default='')
start_time = models.DateTimeField()
image = models.ImageField(null=True, blank=True, help_text=image_help_text)
Here is the traceback:
Traceback (most recent call last):
File "/Applications/XAMPP/xamppfiles/htdocs/enfield/enfield-presents/venv/lib/python3.4/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/Applications/XAMPP/xamppfiles/htdocs/enfield/enfield-presents/venv/lib/python3.4/site-packages/django/db/backends/sqlite3/base.py", line 337, in execute
return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: no such column: posts_event.social_media_image
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 "/Applications/XAMPP/xamppfiles/htdocs/enfield/enfield-presents/venv/lib/python3.4/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
utility.execute()
File "/Applications/XAMPP/xamppfiles/htdocs/enfield/enfield-presents/venv/lib/python3.4/site-packages/django/core/management/__init__.py", line 341, in execute
django.setup()
File "/Applications/XAMPP/xamppfiles/htdocs/enfield/enfield-presents/venv/lib/python3.4/site-packages/django/__init__.py", line 27, in setup
apps.populate(settings.INSTALLED_APPS)
File "/Applications/XAMPP/xamppfiles/htdocs/enfield/enfield-presents/venv/lib/python3.4/site-packages/django/apps/registry.py", line 115, in populate
app_config.ready()
File "/Applications/XAMPP/xamppfiles/htdocs/enfield/enfield-presents/source/enfield_presents/posts/apps.py", line 37, in ready
search.register(Event.objects.get_upcoming_events(site_id=settings.SITE_ID, include_spektrix=False, return_queryset=True))
File "/Applications/XAMPP/xamppfiles/htdocs/enfield/enfield-presents/source/enfield_presents/posts/models.py", line 282, in get_upcoming_events
events_with_a_next_start_time = [e for e in events if e.next_start_time() is not None]
File "/Applications/XAMPP/xamppfiles/htdocs/enfield/enfield-presents/venv/lib/python3.4/site-packages/django/db/models/query.py", line 256, in __iter__
self._fetch_all()
File "/Applications/XAMPP/xamppfiles/htdocs/enfield/enfield-presents/venv/lib/python3.4/site-packages/django/db/models/query.py", line 1087, in _fetch_all
self._result_cache = list(self.iterator())
File "/Applications/XAMPP/xamppfiles/htdocs/enfield/enfield-presents/venv/lib/python3.4/site-packages/django/db/models/query.py", line 54, in __iter__
results = compiler.execute_sql()
File "/Applications/XAMPP/xamppfiles/htdocs/enfield/enfield-presents/venv/lib/python3.4/site-packages/django/db/models/sql/compiler.py", line 835, in execute_sql
cursor.execute(sql, params)
File "/Applications/XAMPP/xamppfiles/htdocs/enfield/enfield-presents/venv/lib/python3.4/site-packages/django/db/backends/utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/Applications/XAMPP/xamppfiles/htdocs/enfield/enfield-presents/venv/lib/python3.4/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/Applications/XAMPP/xamppfiles/htdocs/enfield/enfield-presents/venv/lib/python3.4/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/Applications/XAMPP/xamppfiles/htdocs/enfield/enfield-presents/venv/lib/python3.4/site-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/Applications/XAMPP/xamppfiles/htdocs/enfield/enfield-presents/venv/lib/python3.4/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/Applications/XAMPP/xamppfiles/htdocs/enfield/enfield-presents/venv/lib/python3.4/site-packages/django/db/backends/sqlite3/base.py", line 337, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such column: posts_event.social_media_image
This issue may be due to the reason that the corresponding Image field may not be present in the database. Please login to the database shell and check whether the corresponding field is there. If it is not there add it manually using SQL query or rollback to a previous safe migration and then make the changes in models.py and generate the migration files.
It's right there:
File "/Applications/XAMPP/xamppfiles/htdocs/enfield/enfield-presents/source/enfield_presents/posts/apps.py", line 37, in ready:
search.register(Event.objects.get_upcoming_events(site_id=settings.SITE_ID, include_spektrix=False, return_queryset=True))
You do a queryset on the source of one app, that as far as I can tell, queries the model of the other app. You shouldn't do queries in app setup if you can avoid it. Use signals if you have to or lazy loading.
If you really have to, then flip the order of the apps. If it's the same app, then yes, don't do it: as you notice, you won't be able to do any migrations.

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