[Django]-Django: OperationalError No Such Table

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!

πŸ‘€Brian

312πŸ‘

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
πŸ‘€the

39πŸ‘

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

πŸ‘€Stryker

37πŸ‘

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.

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.

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

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
πŸ‘€skrstv123

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".

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.

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.

πŸ‘€Bradley

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

πŸ‘€Gakuo

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>
πŸ‘€sage poudel

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.

πŸ‘€Steev James

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

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)
πŸ‘€Yabesh

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)

πŸ‘€Kedar K

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



πŸ‘€quine9997

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.

πŸ‘€mybrid

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.

πŸ‘€Sahan

Leave a comment