107

I was following the first app tutorial from the official Django docs and got this error when trying to save some changes made through the admin page. I did some research on it, but the possible solutions I was able to find, such as migrating the db, simply won't work. Just let me know if you want to see some specific part of my code.

Following is error:

OperationalError at /admin/polls/question/1/change/ no such table: main.auth_user__old Request Method: POST Request URL: http://127.0.0.1:8000/admin/polls/question/1/change/ Django Version: 2.1.4 Exception Type: OperationalError Exception Value: no such table: main.auth_user__old Exception Location: /Users/gfioravante/Projects/test_app/ta_env/lib/python3.7/site-packages/django/db/backends/sqlite3/base.py in execute, line 296 Python Executable: /Users/gfioravante/Projects/test_app/ta_env/bin/python3 Python Version: 3.7.1 Python Path:
['/Users/gfioravante/Projects/test_app/test_app', '/usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python37.zip', '/usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python3.7', '/usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload', '/Users/gfioravante/Projects/test_app/ta_env/lib/python3.7/site-packages'] Server time: Wed, 5 Dec 2018 16:45:00 +0000

and the traceback:

Environment:

Request Method: POST Request URL: http://127.0.0.1:8000/admin/polls/question/1/change/

Django Version: 2.1.4 Python Version: 3.7.1 Installed Applications: ['polls.apps.PollsConfig', 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles'] Installed Middleware: ['django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware']

Traceback:

File "/Users/gfioravante/Projects/test_app/ta_env/lib/python3.7/site-packages/django/db/backends/utils.py" in _execute 85. return self.cursor.execute(sql, params)

File "/Users/gfioravante/Projects/test_app/ta_env/lib/python3.7/site-packages/django/db/backends/sqlite3/base.py" in execute 296. return Database.Cursor.execute(self, query, params)

The above exception (no such table: main.auth_user__old) was the direct cause of the following exception:

File "/Users/gfioravante/Projects/test_app/ta_env/lib/python3.7/site-packages/django/core/handlers/exception.py" in inner 34. response = get_response(request)

File "/Users/gfioravante/Projects/test_app/ta_env/lib/python3.7/site-packages/django/core/handlers/base.py" in _get_response 126. response = self.process_exception_by_middleware(e, request)

File "/Users/gfioravante/Projects/test_app/ta_env/lib/python3.7/site-packages/django/core/handlers/base.py" in _get_response 124. response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/Users/gfioravante/Projects/test_app/ta_env/lib/python3.7/site-packages/django/contrib/admin/options.py" in wrapper 604. return self.admin_site.admin_view(view)(*args, **kwargs)

File "/Users/gfioravante/Projects/test_app/ta_env/lib/python3.7/site-packages/django/utils/decorators.py" in _wrapped_view 142. response = view_func(request, *args, **kwargs)

File "/Users/gfioravante/Projects/test_app/ta_env/lib/python3.7/site-packages/django/views/decorators/cache.py" in _wrapped_view_func 44. response = view_func(request, *args, **kwargs)

File "/Users/gfioravante/Projects/test_app/ta_env/lib/python3.7/site-packages/django/contrib/admin/sites.py" in inner 223. return view(request, *args, **kwargs)

File "/Users/gfioravante/Projects/test_app/ta_env/lib/python3.7/site-packages/django/contrib/admin/options.py" in change_view 1640. return self.changeform_view(request, object_id, form_url, extra_context)

File "/Users/gfioravante/Projects/test_app/ta_env/lib/python3.7/site-packages/django/utils/decorators.py" in _wrapper 45. return bound_method(*args, **kwargs)

File "/Users/gfioravante/Projects/test_app/ta_env/lib/python3.7/site-packages/django/utils/decorators.py" in _wrapped_view 142. response = view_func(request, *args, **kwargs)

File "/Users/gfioravante/Projects/test_app/ta_env/lib/python3.7/site-packages/django/contrib/admin/options.py" in changeform_view 1525. return self._changeform_view(request, object_id, form_url, extra_context)

File "/Users/gfioravante/Projects/test_app/ta_env/lib/python3.7/site-packages/django/contrib/admin/options.py" in _changeform_view 1571. self.log_change(request, new_object, change_message)

File "/Users/gfioravante/Projects/test_app/ta_env/lib/python3.7/site-packages/django/contrib/admin/options.py" in log_change 826. change_message=message,

File "/Users/gfioravante/Projects/test_app/ta_env/lib/python3.7/site-packages/django/contrib/admin/models.py" in log_action 35. change_message=change_message,

File "/Users/gfioravante/Projects/test_app/ta_env/lib/python3.7/site-packages/django/db/models/manager.py" in manager_method 82. return getattr(self.get_queryset(), name)(*args, **kwargs)

File "/Users/gfioravante/Projects/test_app/ta_env/lib/python3.7/site-packages/django/db/models/query.py" in create 413. obj.save(force_insert=True, using=self.db)

File "/Users/gfioravante/Projects/test_app/ta_env/lib/python3.7/site-packages/django/db/models/base.py" in save 718. force_update=force_update, update_fields=update_fields)

File "/Users/gfioravante/Projects/test_app/ta_env/lib/python3.7/site-packages/django/db/models/base.py" in save_base 748. updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)

File "/Users/gfioravante/Projects/test_app/ta_env/lib/python3.7/site-packages/django/db/models/base.py" in _save_table 831. result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)

File "/Users/gfioravante/Projects/test_app/ta_env/lib/python3.7/site-packages/django/db/models/base.py" in _do_insert 869. using=using, raw=raw)

File "/Users/gfioravante/Projects/test_app/ta_env/lib/python3.7/site-packages/django/db/models/manager.py" in manager_method 82. return getattr(self.get_queryset(), name)(*args, **kwargs)

File "/Users/gfioravante/Projects/test_app/ta_env/lib/python3.7/site-packages/django/db/models/query.py" in _insert 1136. return query.get_compiler(using=using).execute_sql(return_id)

File "/Users/gfioravante/Projects/test_app/ta_env/lib/python3.7/site-packages/django/db/models/sql/compiler.py" in execute_sql 1289. cursor.execute(sql, params)

File "/Users/gfioravante/Projects/test_app/ta_env/lib/python3.7/site-packages/django/db/backends/utils.py" in execute 100. return super().execute(sql, params)

File "/Users/gfioravante/Projects/test_app/ta_env/lib/python3.7/site-packages/django/db/backends/utils.py" in execute 68. return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)

File "/Users/gfioravante/Projects/test_app/ta_env/lib/python3.7/site-packages/django/db/backends/utils.py" in _execute_with_wrappers 77. return executor(sql, params, many, context)

File "/Users/gfioravante/Projects/test_app/ta_env/lib/python3.7/site-packages/django/db/backends/utils.py" in _execute 85. return self.cursor.execute(sql, params)

File "/Users/gfioravante/Projects/test_app/ta_env/lib/python3.7/site-packages/django/db/utils.py" in exit 89. raise dj_exc_value.with_traceback(traceback) from exc_value

File "/Users/gfioravante/Projects/test_app/ta_env/lib/python3.7/site-packages/django/db/backends/utils.py" in _execute 85. return self.cursor.execute(sql, params)

File "/Users/gfioravante/Projects/test_app/ta_env/lib/python3.7/site-packages/django/db/backends/sqlite3/base.py" in execute 296. return Database.Cursor.execute(self, query, params)

Exception Type: OperationalError at /admin/polls/question/1/change/ Exception Value: no such table: main.auth_user__old

30 Answers30

86

Get rid of this issue easily maintaining the following steps:

  1. keep django version 2.1.5 (the issue addressed in this version) pip install django==2.1.5
  2. Delete the SQLite db
  3. Migrate again python manage.py makemigrations and then python manage.py migrate
  4. Start the server python manage.py runserver

DONE!

nurealam siddiq
  • 1,567
  • 10
  • 9
  • 2
    This will work perfectly but suppose you don't want to delete the sqlite db file ? – Philip Mutua Oct 29 '19 at 06:03
  • 1
    this works even without deleting the sqlite db (but still migrate). i was working on old projects with 2.0.4 – izzulmakin Dec 21 '19 at 13:57
  • Thanks, this works well. I am using version 2.0.7 and having a problem with saving data on the admin. – xreyc Apr 17 '20 at 10:02
  • 1
    For me I first used: `pip install --upgrade django==2.1.5` Then I used `python manage.py makemigrations` and then `python manage.py migrate` Then I used `python manage.py runserver` Worked like a charm, I didn't have to delete db! – Sidrah Madiha Siddiqui Feb 18 '21 at 20:23
54

I just came across this myself, it looks to be related to https://code.djangoproject.com/ticket/29182. For now, you can just downgrade your version of sqlite to a version prior to 2.6 (e.g. 2.5.1)

bmildren
  • 556
  • 5
  • 2
  • yeah, that was it. It seems Django 2.x.x releases are showing this problem, I went back to 1.11.17 and it's all working just fine, thanks! –  Dec 05 '18 at 20:32
  • 7
    A fix for the issue has now been committed, and it will be included in the next point releases 2.1.5 (expected Jan 1 2019) and 2.0.10 (release date TBD). – Alasdair Dec 12 '18 at 11:32
  • 8
    Not so easy to downgrade macOS system sqlite3 or am I missing something here? My workaround is to install github/master django (where this is fixed). – Alper Dec 14 '18 at 20:08
  • 1
    @Alper I uninstalled current django and then installed django from GitHub. the bug still persists, and I wasn't able to downgrade sqlite3 on my macOS (for example, using brew install sqlite3@3.25.1 ) Any idea? – Richard Miller Dec 16 '18 at 02:47
  • I installed Django master and then nuked my database and redid everything (because I was at the start of a project). That seemed to work. – Alper Dec 17 '18 at 15:55
  • I ran in to this problem on ArchLinux as a recent system upgrade pushed my workstation to sqlite 3.26. The Arch package manager docs explained how to downgrade to a previous version of the package. In my case `pacman -U path/to/cache/sqlite-3.25.3-1-x86_64.pkg.tar.xz` – scvnc Dec 20 '18 at 23:04
  • 5
    After updating make sure to delete the database and rebuild it with the migrations command. – Michel K Dec 23 '18 at 16:45
  • I'm sorry to disappoint you @Alasdair but I've just upgraded to Django 2.1.5, tried again and I'm getting exactly the same error https://dpaste.de/Nz4n – Andrea Grandi Jan 04 '19 at 14:27
  • 1
    Django 2.1.5 and Django 2.0.10 were released today, which should fix the issue. Note that the [release notes](https://docs.djangoproject.com/en/2.1/releases/2.1.5/#bugfixes) say: *You might have to drop and rebuild your SQLite database if you applied a migration while using an older version of Django with SQLite 3.26 or later.* – Alasdair Jan 04 '19 at 15:28
  • @AndreaGrandi I would try dropping and rebuilding your SQLite database as the release notes suggest. Back up the sqlite3 file first if it contains any important data. – Alasdair Jan 04 '19 at 15:30
  • 1
    @Alasdair thanks! Deleting and recreating the DB fixed the problem. – Andrea Grandi Jan 04 '19 at 16:23
  • 1
    how can one downgrade to a previous version? it pip freeze doesn't show the version of the sqlite – Alvaro Jan 10 '19 at 19:38
  • 9
    @Alvaro The bug is fixed in Django 2.1.5 – Selcuk Jan 15 '19 at 23:24
20

Just did this and it resolved the problem:

pip install Django --upgrade

Then:

python manage.py migrate

python manage.py makemigrations app

python manage.py migrate
10 Rep
  • 2,217
  • 7
  • 19
  • 33
zurbaranf
  • 201
  • 2
  • 3
  • Can you explain what exactly happened here? It worked for me, but i didn't understood what happened –  Feb 08 '21 at 08:12
  • Just upgraded the Django to the latest version and then migrated again. @NetranjitBorgohain – Asir Shahriar Roudra Feb 09 '21 at 03:26
  • Okay,i thought there was something else happening too. I am very new to this –  Feb 10 '21 at 07:45
  • This fixed it for me. I was using `Django==2.0.7` as suggested in a [tutorial](https://www.youtube.com/watch?v=F5mRW0jo-U4), switched to `3.2.9` by running these commands and it's fixed. – Amir Shabani Nov 13 '21 at 08:28
14

Here is what I did to solve this problem:

  1. Go to the virtual environment and install django@2.1.7

    pip install django==2.1.7
    
  2. Delete the db.sqlite3 file in your root folder.

  3. Create the new db.sqlite3 in your root folder.
  4. Re-run migrations:

    python3 manage.py makemigrations
    
    python3 manage.py migrate
    

Now it should be working all right.

galoget
  • 722
  • 9
  • 15
MING WU
  • 2,962
  • 1
  • 12
  • 16
  • "delete the db.sqlite3 file in your root folder" is the key~ run **python3 manage.py makemigrations** will create new sqlite3 file. – Jerome Dec 29 '19 at 08:29
  • For some reason deleting the sqlite didn't just work, I tried deleting pycache + migrations as well(I am just learning Django), then it worked for me. – Anshuman Kumar Jul 29 '20 at 09:11
13

The problem is caused by the modified behaviour of the ALTER TABLE RENAME statement in SQLite 3.26.0 (see compatiblity note). They also introduced the PRAGMA legacy_alter_table = ON statement in order to maintain the compatibility with previous versions. The upcoming Django release 2.1.5 utilizes the previously mentioned statement as a hotfix. It's expected on January 1, 2019.

Imre Kis
  • 171
  • 3
10

go to this folder django/db/backends/sqlite3

backup schema.py file to another folder

open the original schema.py in a text editor

there you can see a code snippet like

    def __enter__(self):
    # Some SQLite schema alterations need foreign key constraints to be
     # disabled. Enforce it here for the duration of the schema edition.
     if not self.connection.disable_constraint_checking():
         raise NotSupportedError(
             'SQLite schema editor cannot be used while foreign key '
             'constraint checks are enabled. Make sure to disable them '
             'before entering a transaction.atomic() context because '
             'SQLite3 does not support disabling them in the middle of '
             'a multi-statement transaction.'
         )
     self.connection.cursor().execute('PRAGMA legacy_alter_table = ON')
     return super().__enter__()

comment them and paste the following code snippet

     def __enter__(self):
    # Some SQLite schema alterations need foreign key constraints to be
    # disabled. Enforce it here for the duration of the transaction.
    self.connection.disable_constraint_checking()
    self.connection.cursor().execute('PRAGMA legacy_alter_table = ON')
    return super().__enter__()

This worked for me. (the backup for the schema.py is in case the work go wrong ; D )

for more info

https://github.com/django/django/pull/10733/commits/c8ffdbe514b55ff5c9a2b8cb8bbdf2d3978c188f#diff-0c8f495bfee773ab7b5409533bd6d7ef

Trect
  • 2,759
  • 2
  • 30
  • 35
  • 1
    I used this fix for django 1.11, except I only had to add the line "c.execute('PRAGMA legacy_alter_table = ON')" after "c.execute('PRAGMA foreign_keys = 0')" and that fixed it. – n00b May 09 '19 at 15:21
  • 2
    I'm in the middle of a tutorial that requires django 2.0.7. This solution works :) – Bisonbleu Jul 05 '19 at 17:59
  • @Bisonbleu I think I am in the middle of the same tutorial. But the solution did not work for me. Could you elaborate what you did? – Bear Bile Farming is Torture Jan 04 '20 at 18:20
  • 2
    @unathletic_coder as suggested in Namal Jayasundara's answer, I went to /project_name/lib/python3.7/site-packages/django/db/backends/sqlite3/schema.py at line 21 which starts with: def __enter__(self): and replaced the existing code with the suggested 3 lines of code. I'm doing https://www.youtube.com/watch?v=F5mRW0jo-U4 – Bisonbleu Jan 06 '20 at 02:33
7
  1. First, stop the server and delete db.sqlite3.

  2. Then, you need to run:

    python manage.py makemigrations
    python manage.py migrate

  3. After running this command you need to create super user. To Create Super User, run:
    python manage.py createsuperuser
    Enter the super user details there.

  4. Run your server again.

There you go.

Tms91
  • 3,456
  • 6
  • 40
  • 74
Milan Adhikari
  • 119
  • 1
  • 4
6

I solved the problem by upgrading Django from 2.1.4 to 2.1.5 by running

pip install --upgrade django==2.1.5

but I had to rebuild the project anew, because the bug seems to be somehow related to the objects I inserted into the database by using the old version of Django.


UPDATE:

Instead of deleting the entire project, it was sufficient to delete only the database. And then to run

python manage.py makemigrations
python manage.py migrate
python manage.py createsuperuser
Tms91
  • 3,456
  • 6
  • 40
  • 74
6

In my case, it was because of my django version (that was 2.1)

  • Install higher version (2.1.5+ or higher)

  • Delete db.sqlite3, and everything in migration folder except __init__.py

  • Run these commands:

pip install django==2.1.5 --upgrade
python manage.py makemigrations
python manage.py migrate
python manage.py createsuperuser
python manage.py runserver
Arsham Arya
  • 1,461
  • 3
  • 17
  • 25
5

Even after upgrading to the latest Django 2.2.12 and running either migrate or the official database rebuild script, I got the same error with __old_: django.db.utils.IntegrityError: The row in table 'djangocms_blog_post_translation' with primary key '2' has an invalid foreign key: djangocms_blog_post_translation.master_id contains a value '2' that does not have a corresponding value in djangocms_blog_post__old.id.

Here's my hack:

  1. dump the database contents to sql: sqlite3 my_db.db .dump > my_db.sql
  2. run a regex over the sql output, replacing __old" ("id") with " ("id") DEFERRABLE INITIALLY DEFERRED
  3. delete the old database file
  4. load the modified sql into a new database: sqlite3 my_db.db < my_db.sql
  5. ??? & profit
tehfink
  • 447
  • 8
  • 7
  • A thousand times thank you - no way I was deleting my database and starting from scratch. I also had to add PRAGMA legacy_alter_table=FALSE; to the top of my_db.sql – tschumann Feb 20 '21 at 06:29
  • This is a good answer if "just delete your database" is not what you want to do! – Mark Bailey Mar 11 '21 at 12:39
4

Same thing is happened to me, very frustrating. I use anaconda for my environments, I found that I couldn't remove sqlite without immediately reinstalling the most up to date version of sqlite. Trying an older version of django also didn't seem to work. The only solution that has worked for me is by using a PostgreSQL database. It's certainly not ideal but I am planning on utilising the PostgreSQL database in the future so this wasn't a complete waste of time. If you find yourself in the same place as I was then this video may be helpful if you want to know how to connect the PostgreSQL database with your django project.

You'll need to install the postgreSQL database before actually making the changes in settings.py, the installation is more of less clicking Next on all the options. However, remember the username and password you use during installation.

Ithomson90
  • 51
  • 3
4

keep django version 2.1.5

This issue is adressed only in this version of Django
  • pip install django==2.1.5
  • Delete the SQLite db
  • Run migration
  • Start the server python manage.py runserver

This solves the above issue

Amrit Prasad
  • 373
  • 6
  • 18
2

Open => /YourAppFolder/migrations/ You would to see the migrating files just like '0001_initial.py' delete all of these files. And run the follwing command 1- python manage.py makemigrations 2- python manage.py migrate Hope, it must solve your problem

Krishna Kamal
  • 751
  • 2
  • 10
  • 21
Usama Nadeem
  • 128
  • 1
  • 5
  • 1
    in the end the issue wasn't related to that, but thanks for taking the time! –  Dec 05 '18 at 20:34
2

For others who don't want to downgrade any software, you can head into your settings.py file and in the DATABASES dict, you can replace .sqlit3 with .postgresql, and right underneath it change the db.sqlit3 to db.sql. This switches your default db to using postgreSQL.

In doing so, you'll need to pip install psycopg2.

Delete your db.sqlite3 file (if you have one/don't care about losing what's in it) and everything else that isn't the __init__.py file in your app's migration folder. Once you've done all of that, you can run python manage.py makemigrations and python manage.py migrate and then it should work :)

Hope I was able to help someone!

Tms91
  • 3,456
  • 6
  • 40
  • 74
Ziiik
  • 45
  • 4
  • 2
    PostgreSQL also needs to be installed for this to work for anyone who can't figure out why you're getting connection errors after doing all of this. :) – Gilbert Jan 05 '19 at 02:53
  • is psycopg2 not PostgreSQL? Cause that would explain why it wouldn't start a server for me haha – Ziiik Jan 06 '19 at 04:23
  • 3
    AFAIK psycopg2 is an adapter to get Python to talk to PostgreSQL, but PostgreSQL needs to be installed separately. Note: I am new to Django so take anything I say with a grain of salt. – Gilbert Jan 06 '19 at 18:13
2

For the above problem and solution is:

1) Go to the terminal and type in pip install django==2.1.7 or the latest version of django

2) After the installation,In the terminal type in python manage.py makemigrations,and then python manage.py migrate

3) In the terminal,Start the server with code python manage.py runserver

4) Login to the admin server with the password and the add the product,It will successfully add the product.

galoget
  • 722
  • 9
  • 15
  • Welcome to Stack Overflow! Please disclose any [affiliations](https://stackoverflow.com/help/promotion) and do not use the site as a way to promote your site through posting. See [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer). –  Sep 17 '19 at 07:19
1

I had the same issue except that I had 2 sqlite databases and custom database router. I managed to get it working by downgrading Django to 1.11.20 and no need to recreate the databases.

1

you need to upgrade Django, this issue has been fixed in this PR https://github.com/django/django/pull/10733

1

First, update django version:

pip install django --upgrade

then

./manage.py makemigration
./manage.py migrate
./manage.py runserver

Tms91
  • 3,456
  • 6
  • 40
  • 74
pysaundary
  • 166
  • 2
  • 13
0

Steps:

  1. Uninstall current Django from your ENV. Just delete the folder "anaconda3/envs/yourenv/lib/python3.7/site-packages/Django all versions..

    Note: Only for Anaconda users, other users should figure out how to uninstall a package from your ENV.

  2. Go to https://github.com/django/django.

  3. Download the repo as zip file.

  4. Extract zip.

  5. Switch to your ENV.

  6. Enter the extracted folder.

  7. Run "python setup.py install" And install Django.

  8. Delete your previous db.sqlite3 file. Now apply the migrations again to create a new db.sqlite3 file.

    Note: I don't know how to fix previous dbfile and prevent data loss. So please tell me if you know.

  9. Run Server.

Congrats! It works fine now.

Update to latest django in January from the official Django release.

iamzeid
  • 104
  • 1
  • 2
  • 18
0

There are just 4 things I did on command line and it fixed mine.

  1. ctrl + c (stop server)
  2. py manage.py makemigrations
  3. py manage.py migrate
  4. py manage.py runserver (start server)
barbsan
  • 3,418
  • 11
  • 21
  • 28
Nellymandela
  • 109
  • 2
  • 11
  • 1
    The issue has already been fixed with 2.1.5 point release (ticket number #29182 in the "Bugfixes session of the release notes). https://docs.djangoproject.com/en/2.1/releases/2.1.5/#bugfixes –  Feb 19 '19 at 17:46
0

For those who cannot resolve this error with above answers, if you had made your app with its name “main”, this error may occur cause of same app name issue. So try to change your app name “main” to another.

Jinho Park
  • 11
  • 5
0

I solved the problem by changing some of my models. I had one named project and one named projects. The database tables got confused and threw me this error.

Peter Smiley
  • 89
  • 1
  • 7
0

I have solved this issue using below :

1) Delete the db.sqlit3

2) app's directory delete everything in pycache

3) manage.py makemigrations, manage.py migrate, manage.py createsuperuser and then manage.py runserver.

Yogesh Nikam Patil
  • 1,192
  • 13
  • 18
0

I had the same issue and fixed it by doing the below:

1) Get the latest django version

2) get the latest SQL Lite version

3) delete db.sqlite3 file from your project

4) Make a small change to the models.py (e.g. change the size of a field)

5) generate a new db.sqllite3 file by running the makemigrations & migrate commands

6) import the newly created db.sqllite3 file into SQL Lite

Kalana
  • 5,631
  • 7
  • 30
  • 51
0

I installed/downgraded django to 2.2 version ,this removed django 3.x

pip install django==2.2

and then I deleted db.sqlite file and then

I tried

python manage.py makemigrations,
python manage.py migrate 
python manage.py creatingsuperuser. 
nofoobar
  • 2,826
  • 20
  • 24
0
  1. Upgrade Django pip install Django --upgrade

  2. Delete the db.sqlite3 file the root folder of your project.

  3. Create the new db.sqlite3 in your root folder by re-running migrations. python3 manage.py makemigrations

  4. Migrate python3 manage.py migrate

It may work with just this or you may need to create a new superuser if it was deleted.

  1. Check for your superuser using
python manage.py shell
from django.contrib.auth.models import User
User.objects.filter(is_superuser=True)   

If <QuerySet []> appears there is no user. Else check if your user exists.

  1. If your user does not exist create one using python manage.py createsuperuser
Tms91
  • 3,456
  • 6
  • 40
  • 74
WOLF CRECENT
  • 201
  • 3
  • 6
-1

Please check if you haven't deleted the migration folder from your app if deleted try to restore the folder and remove migration files or if deleted permanently create app and copy paste your work and

then

1. Delete db.sqlite3
2. python manage.py makemigrations
3. python manage.py migrate
4. python manage.py createsuperuser 

HTH :)

Mebatsion Sahle
  • 409
  • 2
  • 9
-1
  1. Delete db.sqlite3
  2. makemigrations & migrate
  3. Create new super user

This works for me

MD SHAYON
  • 7,001
  • 45
  • 38
-1

Note: Do not follow this trick if you have some personal data in Sqlite3 DB as you're going to delete Sqlite3 DB

I know many answers are given to this question but only this trick help me to solve this issue as I'm beginner at python and learning Django.

  1. Stopped the django webserver running, Ctrl-C
  2. Delete the db.sqlite3
  3. Uninstalled Django old verison
  4. Install latest version of Django with “pip install django”
  5. Delete all migrations from all apps of your project

Now run these commands in terminal

  1. python manage.py makemigrations
  2. python manage.py migrate
  3. python manage.py createsuperuser ( provide super-user credentials )
  4. python manage.py runserver

Now finally login to admin panel with updated super-user's credentials and try to add record.

Same suggestions at this link by someone

msayubi76
  • 1,446
  • 1
  • 12
  • 18
-2

django-2.2.7
This worked for me -

1) Delete db.sqlite3.
2) Within each app, within the migrations folder, delete everything other than __init__.py .
3) Within each app,delete __pycache__ folder.

I am not sure if you had to do it for all apps or just the concerned app, but this worked for me.

Loner
  • 166
  • 1
  • 4
  • 10