Django: OperationalError No Such Table
Asked Answered
K

19

121

I'm building a fairly simple application, research, in my Django project that uses Django-CMS. (It's my first ground-up attempt at a project/application.) Its main purpose is to store various intellectual assets (i.e article, book, etc. written by a researcher).

The problem is that when I point the browser to /research/ I get an error saying that the table 'research_journal' doesn't exist ("no such table").

I'm using Djnago 1.6.5 with a sqlite3 database.

Looking at python manage.py sql research yields:

BEGIN;
CREATE TABLE "research_researchbase" (
    "id" integer NOT NULL PRIMARY KEY,
    "pub_date" datetime NOT NULL,
    "authors" varchar(200) NOT NULL,
    "year" varchar(25) NOT NULL,
    "title" varchar(200) NOT NULL,
    "subtitle" varchar(200) NOT NULL,
    "image_id" integer NOT NULL REFERENCES "filer_image" ("file_ptr_id"),
    "link" varchar(200) NOT NULL
)
;
CREATE TABLE "research_journal" (
    "researchbase_ptr_id" integer NOT NULL PRIMARY KEY REFERENCES "research_researchbase" ("id"),
    "journal" varchar(200) NOT NULL,
    "abstract" text NOT NULL,
    "citation" varchar(200) NOT NULL
)
;
CREATE TABLE "research_encyclopedia_chapter" (
    "researchbase_ptr_id" integer NOT NULL PRIMARY KEY REFERENCES "research_researchbase" ("id"),
    "encyclopedia" varchar(200) NOT NULL,
    "publisher" varchar(200) NOT NULL,
    "summary" varchar(200) NOT NULL
)
;
CREATE TABLE "research_book" (
    "researchbase_ptr_id" integer NOT NULL PRIMARY KEY REFERENCES "research_researchbase" ("id"),
    "publisher" varchar(200) NOT NULL,
    "summary" varchar(200) NOT NULL
)
;

COMMIT;

I've run python manage.py migrate research and get:

/Users/XXX/Documents/repos/sfs/env/lib/python2.7/site-packages/app_data/fields.py:2: DeprecationWarning: django.utils.simplejson is deprecated; use json instead.
  from django.utils import simplejson as json

Running migrations for research:
- Nothing to migrate.
 - Loading initial data for research.
Installed 0 object(s) from 0 fixture(s)

I've run python manage.py syncdb and get the following:

Syncing...
Creating tables ...
Installing custom SQL ...
Installing indexes ...
Installed 0 object(s) from 0 fixture(s)

Synced:
 > djangocms_admin_style
 > django.contrib.auth
 > django.contrib.contenttypes
 > django.contrib.sessions
 > django.contrib.admin
 > django.contrib.sites
 > django.contrib.sitemaps
 > django.contrib.staticfiles
 > django.contrib.messages
 > mptt
 > south
 > sekizai
 > django_select2
 > hvad

Not synced (use migrations):
 - djangocms_text_ckeditor
 - cms
 - menus
 - djangocms_style
 - djangocms_column
 - djangocms_file
 - djangocms_flash
 - djangocms_googlemap
 - djangocms_inherit
 - djangocms_link
 - djangocms_picture
 - djangocms_teaser
 - djangocms_video
 - reversion
 - polls
 - djangocms_polls
 - aldryn_blog
 - easy_thumbnails
 - filer
 - taggit
 - research
(use ./manage.py migrate to migrate these)

Here's the models.py:

from django.db import models
from django.utils import timezone
from filer.fields.image import FilerImageField

import datetime

class ResearchBase(models.Model):
    pub_date = models.DateTimeField('date published')
    authors = models.CharField(max_length=200)
    year = models.CharField(max_length=25)
    title = models.CharField(max_length=200)
    subtitle = models.CharField(max_length=200, blank=True)
    image = FilerImageField()
    link = models.CharField(max_length=200, blank=True)
    
    def __unicode__(self):
        return self.title
    
    def was_published_recently(self):
        return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
        

class Journal(ResearchBase):
    journal = models.CharField(max_length=200)
    abstract = models.TextField()
    citation = models.CharField(max_length=200)
    
    
class Encyclopedia_Chapter(ResearchBase):
    encyclopedia = models.CharField(max_length=200)
    publisher = models.CharField(max_length=200)
    summary = models.CharField(max_length=200)
    
        
class Book(ResearchBase):
    publisher = models.CharField(max_length=200)
    summary = models.CharField(max_length=200)

Here's my views.py (note that I am passing two objects through render, ignore the fact that I have yet to include the class Books in the whole deal):

from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse, Http404
from django.template import RequestContext, loader

from research.models import Journal, Encyclopedia_Chapter, Book

def research_index(request):
    latest_journal_list = Journal.objects.order_by('-pub_date')[:5]
    latest_chapter_list = Encyclopedia_Chapter.objects.order_by('-pub_date')[:5]
    
    context = {
        'latest_journal_list': latest_journal_list,
        'latest_chapter_list': latest_chapter_list
    }
    
    return render(request, 'research/index.html', context)
    
def journal_detail(request, journal_id):
    journal = get_object_or_404(Journal, pk=journal_id)
    return render(request, 'research/journal_detail.html', {'journal': journal})
    
def chapter_detail(request, chapter_id):
    chapter = get_object_or_404(Encyclopedia_Chapter, pk=chapter_id)
    return render(request, 'research/chapter_detail.html', {'chapter': chapter})

Here's the application's url.py:

from django.conf.urls import patterns, url

from research import views

urlpatterns = patterns('',
    url(r'^$', views.research_index, name='research'),
    url(r'^(?P<journal_id>\d+)/$', views.journal_detail, name='journal_detail'),
    url(r'^(?P<chapter_id>\d+)/$', views.chapter_detail, name='chapter_detail'),
)

Here's the index.html template:

{% extends 'research/base.html' %}

{% block research_content %}

<div class="container">
    <div class="row featurette">
        <h3 id="research">Peer-reviewed Journal Articles</h3>
        {% if latest_journal_list %}
            <ul id="research">
            {% for journal in latest_journal_list %}
                <li id="research">
                            <img src="{{ journal.image.url }}" id="research">
                            <h4>{{ journal.journal }}</h4>
                            <h5>{{ journal.title }}</h5>
                            <a href="{% url 'research:journal_detail' journal.id %}">Read More</a>
                        </li>
            {% endfor %}
            </ul>
        {% else %}
            <p>No journals are available.</p>
        {% endif %}
    </div>
    
    <div class="row featurette">
        <h3 id="research">Encyclopedia Chapters</h3>
        {% if latest_chapter_list %}
            <ul id="research">
            {% for chapter in latest_chapter_list %}
                <li id="research">
                            <img src="{{ chapter.image.url }}" id="research">
                            <h4>{{ chapter.journal }}</h4>
                            <h5>{{ chapter.title }}</h5>
                            <a href="{% url 'research:chapter_detail' chapter.id %}">Read More</a>
                        </li>
            {% endfor %}
            </ul>
        {% else %}
            <p>No encyclopedia chapters are available.</p>
        {% endif %}
    </div>
</div>

{% endblock %}

Just in case it matters, here's my cms_app.py:

from cms.app_base import CMSApp
from cms.apphook_pool import apphook_pool
from django.utils.translation import ugettext_lazy as _


class ResearchApp(CMSApp):
    name = _("Research App")
    urls = ["research.urls"]
    app_name = "research"

apphook_pool.register(ResearchApp)
Kudos answered 10/9, 2014 at 17:42 Comment(7)
Did you run python manage.py syncdb?Purlin
Yes. I've run python manage.py syncdb (added result to post). I was under the impression, however that using south via migrate eliminated the need to sync the database through the aforementioned command. Is that incorrect?Kudos
Did you try to drop the DB and syncDB again?Submit
I didn't want to drop the whole DB and lose other tables (and their data). I tried ./manage.py sqlclear research | ./manage.py dbshell followed by ./manage.py syncdb, which cleared research's table. Ultimately, that didn't work either. See my solution below...Kudos
Even if you added South to the project, you still have to run syncdb at first run, only after that you can drop this command.Purlin
The last one worked for me in dJango 2.2.3 ... doing makemigrations followed by the specific app name.Pyszka
This was something I had to do #71386257Townes
K
25

It looks like there was an issue with my migration.

I ran ./manage.py schemamigration research --auto and found that many of the fields didn't have a default specified.

So, I ran ./manage.py schemamigration research --init followed by ./manage.py migrate research

Rerunning the server from there did the trick!

Kudos answered 10/9, 2014 at 19:22 Comment(2)
A comment has more likes, then the answer itself. @Brian, please update the answer with this useful information.Been
It says Unknown command: 'schemamigration'. Did you mean showmigrations? for meLogging
F
326

Use:

python manage.py migrate --run-syncdb 

As stated in this comment by Benyamin Jafari:

--run-syncdb - Creates tables for apps without migrations.

Also don't forget to specity app path. For example:

python manage.py makemigrations app
python manage.py migrate app
Finecut answered 13/6, 2016 at 22:4 Comment(7)
Don't forget to run python makemigrations firstFoxing
what is the --run-syncdb option?Flagelliform
I found it: --run-syncdb --> Creates tables for apps without migrations.Flagelliform
This worked, but I had to remove the .py and .pyc migration files from <app_dir>/migrations before running makemigrations and the above command.Eliathan
I ran this command, and the OperationalError was still thrown. I also deleted my db.sqlite3 & migrations folder, but still getting the same error.Newspaper
Thanks, it really worked. But what I do not understand: Why didn't I need this in the first two years where I used Django ever? It worked 2 years all the time without this --run-syncdb and suddenly I got the error no such table out of nowhere...Masurium
I got this error when adding default = 1, with a OneToOneField. Where do I find the solution for this, without needing to delete what I already have. The reason I get this error is because default 1, added the id 1 to more than 1 object, then it is not unique. I just realised I can make it a ForeignKey, and then delete it in the Django admin.Townes
O
39

For django 1.10 you may have to do python manage.py makemigrations appname.

Overabound answered 23/11, 2016 at 16:13 Comment(0)
E
39

If anyone finds that any of the suggested:

python manage.py makemigrations
python manage.py migrate
python manage.py migrate --run-syncdb

fail, you may need to add a folder named "migrations" inside the app directory, and create an empty __init__.py file.

Eby answered 3/5, 2019 at 8:14 Comment(2)
I accidentally delete init.py, so got the errorShanan
accidentally delete init.py, so got the error.Macnamara
K
25

It looks like there was an issue with my migration.

I ran ./manage.py schemamigration research --auto and found that many of the fields didn't have a default specified.

So, I ran ./manage.py schemamigration research --init followed by ./manage.py migrate research

Rerunning the server from there did the trick!

Kudos answered 10/9, 2014 at 19:22 Comment(2)
A comment has more likes, then the answer itself. @Brian, please update the answer with this useful information.Been
It says Unknown command: 'schemamigration'. Did you mean showmigrations? for meLogging
T
10

The issue may be solved by running migrations.

  1. python manage.py makemigrations
  2. python manage.py migrate

perform the operations above whenever you make changes in models.py.

Tibbs answered 22/6, 2018 at 19:6 Comment(0)
B
6

This error comes when you have not made migrations to your newly created table, So,firsty write command on cmd as: python manage.py makemigrations and then write another command for applying these migrations made by makemigrations command: python manage.py migrate

Beaujolais answered 28/10, 2018 at 5:46 Comment(0)
D
5

Running the following commands solved this for me

  1. python manage.py migrate
  2. python manage.py makemigrations
  3. python manage.py makemigrations appName
Driggers answered 23/6, 2019 at 9:55 Comment(0)
P
4

Run this command below:

"migrate" with "--run-syncdb" creates tables for apps without migrations.

python manage.py migrate --run-syncdb 

This is the full description about "migrate" with "--run-syncdb":

--run-syncdb

Allows creating tables for apps without migrations. While this isn’t recommended, the migrations framework is sometimes too slow on large projects with hundreds of models.

You can check the Django documentation about "migrate" with "--run-syncdb".

Polish answered 2/3, 2022 at 12:3 Comment(0)
W
3

I got through the same error when I went on to the admin panel. You ought to run this instead-: python manage.py migrate --run-syncdb. Don't forget to include migrate, I ran:

python manage.py make migrations and then python manage.py migrate

Still when the error persisted I tried it with the above suggested command.

Wildfowl answered 20/10, 2018 at 17:37 Comment(0)
S
2

I'm using Django 1.9, SQLite3 and DjangoCMS 3.2 and had the same issue. I solved it by running python manage.py makemigrations. This was followed by a prompt stating that the database contained non-null value types but did not have a default value set. It gave me two options: 1) select a one off value now or 2) exit and change the default setting in models.py. I selected the first option and gave the default value of 1. Repeated this four or five times until the prompt said it was finished. I then ran python manage.py migrate. Now it works just fine. Remember, by running python manage.py makemigrations first, a revised copy of the database is created (mine was 0004) and you can always revert back to a previous database state.

Satiable answered 26/4, 2016 at 21:26 Comment(0)
T
2

If you get to the bottom of this list and find this answer, I am almost sure it will solve all your issues :) In my case, I had dropped a database table and I was not getting anywhere with makemigrations and migrate

So I got a very detailed answer on how to reset everything on this link

Trotta answered 26/8, 2020 at 12:26 Comment(0)
B
2

The Thing that worked for me:

  1. Find out which migrations in your migration folder created the table if not add the class in your models.py.
  2. If the class already exist in your models.py, try to delete that one and run python manage.py makemigrations <appname>
  3. And while migrating fake that migrations as your error might say table not found to delete using python manage.py migrate <yourappname> --fake
  4. Add the class again and makemigrations again python manage.py makemigrations <appname>.
  5. And finally migrate again python manage.py migrate <appname>
Behemoth answered 2/9, 2020 at 16:20 Comment(0)
L
1

This happened to me and for me it was because I added db.sqlite3 as untracked from repository. I added it and pushed it to server so it worked properly. Also run makemigartions and migrate after doing this.

Lanfri answered 25/8, 2018 at 13:28 Comment(0)
T
1

In my case, it was solved by resetting the DB (dev environment actually), by running the reset_db command from Django-Extensions :

python manage.py reset_db

After that I ran the following commands :

  1. python manage.py makemigrations
  2. python manage.py migrate
Tenderize answered 3/7, 2021 at 5:51 Comment(2)
Phenomenal Django extensions - thanks for that. But it didn't solve the problem on my end.Woadwaxen
Work for me. Be care that all data will be deleted.Micropyle
G
1

Close the Terminal and again open it and run the following commands:

  1. python manage.py migrate (app name)
  2. python manage.py makemigrations
  3. python manage.py makemigrations (appname)
Gintz answered 23/10, 2021 at 6:31 Comment(0)
S
1

Happened to me. It usually happens when we're doing a lot of changes without checking if each individual changes are correctly applied or not (use migrate and makemigrations after each change in tables/cration of tables).

Now what you can try are -

  1. python manage.py migrate (app name)
  2. python manage.py makemigrations
  3. python manage.py makemigrations (app name)

even if above did not worked then what you can do is - go to migration folder inside your app folder and then delete files that might have caused error(each time you migrate, a new file will be added here to reflect changes, new tables). So find those files and delete those files, which may cause error. And then again apply migrate and makemigrations.

Even if it did not worked, then below code might work.

python manage.py migrate --run-syncdb

Even if above three things did not worked at last, then you should delete db.sqlite3 file which stores your tables and simply create another db.sqlite3 (of course using vs code or pycharm or any coding environment else computer will create text file). Then after creationg another db.splite3,

python manage.py migrate (app name)

python manage.py makemigrations

python manage.py makemigrations (app name)

Steadman answered 6/7, 2022 at 10:21 Comment(1)
Please upvote if found Helpful😋, also you can ask me any other question without any hesistationSteadman
P
1

Clean and re create everything.

Example.

The root folder of my project is:

/media/ubuntu/shareddir1/django001/Character_SW/Character_SW/

i. Delete your database (db.sqlite3 in my case) in your project directory
/media/ubuntu/shareddir1/django001/Character_SW/Character_SW/

find . | grep -i db.sqlite3
rm -Rvf ./db.sqlite3
rm -Rvf ./*/db.sqlite3
rm -Rvf ./*/*/db.sqlite3
rm -Rvf ./*/*/*/db.sqlite3
rm -Rvf ./*/*/*/*/db.sqlite3
find . | grep -i db.sqlite3



i. Remove everything from __pycache__ folder under your project subdirectory
cd /media/ubuntu/shareddir1/django001/Character_SW/Character_SW/

find . | grep -i pycache
rm -Rvf ./__pycache__
rm -Rvf ./*/__pycache__
rm -Rvf ./*/*/__pycache__
rm -Rvf ./*/*/*/__pycache__
rm -Rvf ./*/*/*/*/__pycache__
find . | grep -i pycache


i. For the application you are trying to fix, go to the folder and clear migrations directory
find . | grep -i migrations
rm -Rvf ./migrations
rm -Rvf ./*/migrations
rm -Rvf ./*/*/migrations
rm -Rvf ./*/*/*/migrations
rm -Rvf ./*/*/*/*/migrations
find . | grep -i migrations

i. re create table etc
cd /media/ubuntu/shareddir1/django001/Character_SW/Character_SW/


python3 manage.py makemigrations
python3 manage.py migrate
python manage.py migrate --run-syncdb 

#python3 manage.py createsuperuser
python3 manage.py runserver



Pairs answered 6/3, 2023 at 3:49 Comment(0)
D
0

I'm using Django CMS 3.4 with Django 1.8. I stepped through the root cause in the Django CMS code. Root cause is the Django CMS is not changing directory to the directory with file containing the SQLite3 database before making database calls. The error message is spurious. The underlying problem is that a SQLite database call is made in the wrong directory.

The workaround is to ensure all your Django applications change directory back to the Django Project root directory when changing to working directories.

Deandra answered 15/9, 2016 at 19:12 Comment(0)
B
0

Even if some solutions are given to this problem, it's still worth mentioning a common mistake why this error appears.

  1. makemigrations create migrations files required. But actual tables are not populated yet in the database.
  2. migrate create tables in the database.

Until these 2 commands successfully completed, there will be no tables in your database.

So, the most common reason for this error is, trying to access some database tables/records before successfully running above 2 commands. Let's take an example.

  • I have a model called Role.
  • And I haven't run makemigrations or migrate commands yet.
  • And i have a forms.py file like below

forms.py

def get_roles():
    roles = [(role, role) for role in Role.objects.all()]
    return roles


class UserRegisterForm(UserCreationForm):
    role = forms.ChoiceField(choices=get_roles())

Now if I try to run makemigrations command, this will throw an exception called OperationalError because in my get_role() function I am trying to load database records with my Role model but, that table doesn't exists in the database yet.

So, when running makemigrations command it read my forms.py file and tried to load data from the model Role, which doesn't exists yet.

To prevent these kinds of errors, make sure you are not access database without handling exceptions. You may be accidentally doing these kinds of mistakes in your models.py, forms.py, views.py or anywhere in your code.

Otherwise you will have to use syncdb command before running makemigrations and migrate commands.

Bechance answered 19/3, 2023 at 8:7 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.