102👍
I solved the same problem with these steps :
- Delete your database (
db.sqlite3
in my case) in your project directory - Remove everything from
__pycache__
folder under your project subdirectory - For the application you are trying to fix, go to the folder and clear
migrations
and__pycache__
directories
When you are sure you have cleared all the above files, run:
python manage.py makemigrations
python manage.py migrate
- [Django]-How do I print out the contents of my settings in a django shell?
- [Django]-What the difference between using Django redirect and HttpResponseRedirect?
- [Django]-How do I extend the Django Group model?
39👍
Another case wihch can generate the no such table error. If your views.py or similar executes code that tries to access the DB when imported, i.e. importing views.py has side effects, then starting from scratch won’t work.
This happens when your code was working with an existing DB, and now you’re trying to start without a DB. Just change views.py so it can be imported without side effects. If you don’t want to fix the design, do something like:
from django.db.utils import OperationalError
format_list = [('', '(all)')]
geom_type_list = [('', '(all)')]
try:
format_list.extend([(i[0],i[0])
for i in Format.objects.values_list('name')])
geom_type_list.extend([(i[0],i[0])
for i in Geom_type.objects.values_list('name')])
except OperationalError:
pass # happens when db doesn't exist yet, views.py should be
# importable without this side effect
- [Django]-Django required field in model form
- [Django]-Plug in django-allauth as endpoint in django-rest-framework
- [Django]-Django model: NULLable field
21👍
To solve it I did this (on Ubuntu, you’ll need to adapt the commands for Windows):
1. Remove all the migration files
find . -path "*/migrations/*.py" -not -name "__init__.py" -delete
find . -path "*/migrations/*.pyc" -delete
2. Delete db.sqlite3
rm db.sqlite3
3. Create and run the migrations:
python manage.py makemigrations
python manage.py migrate
4. Sync the database:
manage.py migrate --run-syncdb
Bit of a pain as you need to delete your database, but fine for a test system.
Got all but the final step from this generally excellent resource: https://simpleisbetterthancomplex.com/tutorial/2016/07/26/how-to-reset-migrations.html
- [Django]-How can I temporarily disable a foreign key constraint in MySQL?
- [Django]-Override save method of Django Admin
- [Django]-Passing objects from Django to Javascript DOM
- [Django]-Select_related with reverse foreign keys
- [Django]-Custom Filter in Django Admin on Django 1.3 or below
- [Django]-Django REST Framework serializer field required=false
10👍
Adding to terry_brown’s answer, this is what cause my problems. I had a custom user model with ForeignKey to another model. And I set defaults to first object in the DB. It worked when I had the data in the database. But when I started from scratch, it didn’t work because it was executed immediately when imported (so not even migration went through).
class User(AbstractBaseUser, PermissionsMixin):
subscription_plan = models.ForeignKey(SubscriptionPlan, default=SubscriptionPlan.objects.first().id)
I had to sacrifice that default
(commenting it out).
UPDATE:
A better solution would be to prepopulate your database with initial migration or fixtures.
- [Django]-How can I render a tree structure (recursive) using a django template?
- [Django]-Django: Grab a set of objects from ID list (and sort by timestamp)
- [Django]-Serializing a list of objects with django-rest-framework
5👍
Follow this steps to get fixed this issue.
python manage.py migrate --fake APPNAME zero
This will make your migration to fake. Now you can run the migrate script
python manage.py migrate APPNAME
OR
python manage.py migrate
Tables will be created and you solved your problem.. Cheers!!!
- [Django]-How to see details of Django errors with Gunicorn?
- [Django]-Django query case-insensitive list match
- [Django]-Permission denied – nginx and uwsgi socket
3👍
I read the above Answers given by other teams.
Majorly were asking to delete SQLite(DB) and reset the "migrations" folder.
If you are working in a production environment then this will not be the right choice.
python manage.py migrate --fake APPNAME zero
python manage.py migrate APPNAME
python manage.py makemigrations APPNAME
python manage.py migrate APPNAME
These four steps are enough to solve this issue.
- [Django]-PIL /JPEG Library: "decoder jpeg not available"
- [Django]-403 Forbidden error when making an ajax Post request in Django framework
- [Django]-How to upgrade django?
3👍
I faced the same problem. I removed all migrations and run this command
python manage.py makemigrations
python manage.py migrate --run-syncdb
It worked for me and the data was also safe (I didn’t lose any)
- [Django]-South migration: "database backend does not accept 0 as a value for AutoField" (mysql)
- [Django]-Speeding up Django Testing
- [Django]-Getting Site Matching Query Does Not Exist Error after creating django admin
2👍
This error is happening because of the database of Django , this is not user fault. Its easiest solution is create new app with different name .Then create same models and then run python manage.py makemigrations and then migrate. By doing this your error should be solved
- [Django]-Count number of records by date in Django
- [Django]-Django: save() vs update() to update the database?
- [Django]-Django – Clean permission table
2👍
Maybe migrations Folder was not created:
python manage.py makemigrations app_name
python manage.py migrate
- [Django]-How to add multiple objects to ManyToMany relationship at once in Django ?
- [Django]-How do I deploy Django on AWS?
- [Django]-Can I use a database view as a model in Django?
1👍
While running any management command “django.contrib.admin” automatically attempts to discover and admin.py modules in installed apps.
If there is a code related to database, it runs automatically and tries to load data from database when it can not find data related table in database throw this Error.
To fix this error just change "django.contrib.admin"
in INSTALLED_APPS
to "django.contrib.admin.apps.SimpleAdminConfig"
, it will prevent “django.contrib.admin” from auto discovering and running admin modules.
django.contrib.admin automatically performs auto discovery of admin modules in installed applications. To prevent it, change your INSTALLED_APPS to contain ‘django.contrib.admin.apps.SimpleAdminConfig’ instead of ‘django.contrib.admin’.
https://docs.djangoproject.com/en/3.0/ref/applications/#troubleshooting
- [Django]-How do I include image files in Django templates?
- [Django]-How to make Django serve static files with Gunicorn?
- [Django]-Django change default runserver port
1👍
You don’t need views to makemigrations. Comment out all references to other apps from urls.py, clear db and old migrations, run makemigrations, and comment your urls.py back in. Worked for me.
- [Django]-Access request in django custom template tags
- [Django]-Django-Forms with json fields
- [Django]-Celery Flower Security in Production
1👍
I had the same problem.
None of the above solutions worked for me.
It all probably started when I added a new model, had a few objects in the database and then I changed model’s name.
I needed to delete the whole model from my app and then run app’s admin page. Then I restored the model by clicking ctrl + z in all files from which I deleted it and finally I was able to run makemigrations and migrate commands.
- [Django]-Django {% with %} tags within {% if %} {% else %} tags?
- [Django]-'staticfiles' is not a valid tag library: Template library staticfiles not found
- [Django]-How can I test binary file uploading with django-rest-framework's test client?
1👍
I was facing the same error. The steps I used to solve this issue is:
-
Make sure that there’s
migrations
folder inside your app. If there isn’t any, create themigrations
folder inside the app folder, and then create__init__.py
file inside the folder. -
If the
migrations
folder is already there, then delete all themigration files
. -
Delete the database file which is by default
db.sqlite3
. -
Run the command
python manage.py makemigrations
-
python manage.py migrate
- [Django]-How do you configure Django for simple development and deployment?
- [Django]-Django Template Ternary Operator
- [Django]-Editing django-rest-framework serializer object before save
0👍
Maybe out of time but…I have same problem when I tried to “clone” Django 1.11 installation into other directory and then with initial try to manage makemigrations.
I solve the problem following way:
- settup initial Django installation and creating main application by:
django-admin.py startproject app_name
-
initial migrations
manage makemigrations, manage migrate -
Setup the superuser:
manage createsuperuser
-
copy all files and directories (Django applications) except the urls.py and settings.py in main directory
-
Added all apps into INSTALLED_APPS
-
manage makemigrations, manage migrate
-
Copied settings.py and urls.py from source Django application directory
Thats it no errors and all is working fine.
Petr
- [Django]-How do I use a dictionary to update fields in Django models?
- [Django]-Overriding the save method in Django ModelForm
- [Django]-Meaning of leading underscore in list of tuples used to define choice fields?
0👍
If someone else has this problem and the accepted solution is not working, have a look at your db path,
the db path should be absolute path,
'NAME': '/pathto-db/default.db',
- [Django]-Django Queryset with year(date) = '2010'
- [Django]-Django Admin: Using a custom widget for only one model field
- [Django]-How to allow only one radio button to be checked?
0👍
If none of the above solution worked for you then add the appname
along with the makemigration
and migrate
command.
makemigration login
migrate login
That might help you to figure out the problem.
- [Django]-Gunicorn autoreload on source change
- [Django]-Django ModelForm with extra fields that are not in the model
- [Django]-Django 1.7 – App 'your_app_name' does not have migrations
0👍
I got this error while creating a model.
I opened the db.sqlite3 file and created a table for the model using SQLite DB Browser. Then I created a migration and fake migrated it. Don’t know what caused the issue but this workaround helped.
- [Django]-Running Django with FastCGI or with mod_python
- [Django]-Difference between filter with multiple arguments and chain filter in django
- [Django]-Django dynamically filtering with q objects
0👍
would you delete a production bank?
I saw that many have the sqlite deleted, but if it is a production bank?
solution to delete data from django_migrations table less contenttypes
already in the table django_content_type you remotely app_label
inside the migrations folder deletes everything but init.py
python manage.py makemigrations
python manage.py migrate –fake
that’s how I solved the app duplication problems (I had two apps with the same name after installing a plugin) and django created the missing tables, among other things.
in my normally functional case
- [Django]-Cannot set Django to work with smtp.gmail.com
- [Django]-Format numbers in django templates
- [Django]-How to add annotate data in django-rest-framework queryset responses?
0👍
With same error and without removing database:
- remove migrations
- remove
__pychache__
python manage.py makemigrations --empty alias
python manage.py makemigrations alias
python manage.py migrate alias zero
- remove again migrations
- remove again
__pycache__
python manage.py makemigrations
python manage.py migrate alias zero
Where alias is the app name.
- [Django]-Django: Open uploaded file while still in memory; In the Form Clean method?
- [Django]-Collectstatic error while deploying Django app to Heroku
- [Django]-Django Rest Framework – Get related model field in serializer
0👍
If you have deleted the DB and migrating now this will happens.
For eg if your error says no such table table1
then
- python manage.py makemigrations
- python manage.py migrate
- python manage.py table1 app1
Here table1 is the name of the table and app1 is the Django app name where the table1 model exists.
- [Django]-How to save pillow image object to Django ImageField?
- [Django]-In Django is there a way to display choices as checkboxes?
- [Django]-Django's ManyToMany Relationship with Additional Fields
0👍
I know this commonly solved by deleting de sqlite3 DB that is used in the development process.
Also, I know this question is currently solved, but I want to leave my case here as an alternative.
Sometimes while we are programming we change a lot of things in our models. When we run python manage.py makemigrations
a file is created inside of our app folder/migrations
. And when we run python manage.py migrate
a record in the table django_migrations is created. this record contains 3 fields we must check in detail:
-
1 – name (this is the file name created in the migrations app folder)
-
2 – app (this is the name of our app. Also works like a namespace to prevent conflicts)
-
3 – applied (this is the date-time when the migration was executed).
If you’ve modified your model previously, you should have several files inside migrations (amount accordingly with the times you’ve executed makemigrations on every change), in my case I wanted to erase these files just to have only one migration file.
The problem with this is that when makemigrations was run again it creates a file 0001_initial.py. If you remember the table django_migrations contains a log of this file, this is to prevent the execution of migrations that already have been done.
If this is your case, you would erase the records of your app migrations from this table.
- [Django]-Auto-create primary key used when not defining a primary key type warning in Django
- [Django]-Can you perform multi-threaded tasks within Django?
- [Django]-Allowing only super user login
0👍
I did some alterations in my database and was facing the same issue. None of solutions worked for me so after spending hours i finally decided to delete the "db.sqlite3" file and runs the makemigrations and migrate commands. After it worked fine. But this solution is not a good one to use immediately, use it only when you are out of options.
- [Django]-Suddenly when running tests I get "TypeError: 'NoneType' object is not iterable
- [Django]-Filter Queryset on empty ImageField
- [Django]-Django logging of custom management commands
0👍
I recently had a problem with this and used the @adam post to resolve my issue. I just wanted to amend his post and say that, for me in windows 10 using git bash, I had to do all of this from the venv, and then after I removed the migration files and the DB I had to pip uninstall django
and pip install django
before making the migrations.
- [Django]-Equivalent of PHP "echo something; exit();" with Python/Django?
- [Django]-How can I filter a date of a DateTimeField in Django?
- [Django]-Django – Website Home Page
0👍
This issue comes up for me when I have a function/class/etc. somewhere that uses fields from the Database — typically when using Django’s Form classes.
Example:
brands = forms.MultipleChoiceField(
choices=[(b.name, b.id) for b in Brand.objects.all()],
widget=forms.CheckboxSelectMultiple()
)
Here I’m creating a MultipleChoiceField
entry for a form using the name
and id
fields from all brand
object entries in a database. i.e. loading all the options a user can select in the database from the database.
When the database is already present and the app_brand
table is already present, there is no issue.
However, when creating migrations for this application initially, I get the Error:
django.db.utils.OperationalError: no such table: app_brand
I don’t have an elegant work-around, but what I usually do is the following:
# default brands
try:
brands = forms.MultipleChoiceField(
choices=[(b.name, b.id) for b in Brand.objects.all()],
widget=forms.CheckboxSelectMultiple()
)
except Exception as e:
brands = forms.MultipleChoiceField(
choices=[],
widget=forms.CheckboxSelectMultiple()
)
This allows the initial python manage.py makemigrations
command to be issued w/o raising the exception. The obvious downside is that there are likely cases that would normally cause the application to fail that might now simply result in an unpopulated form field in a production environment.
TL;DR – It’s an issue sometimes if an application has functions/etc. that access the database such as Forms and those database tables don’t already exist.
- [Django]-Negating a boolean in Django template
- [Django]-Django model blank=False does not work?
- [Django]-Django: How to set a field to NULL?
0👍
I had similar issues, cannot make migrations in no present database etc.. disabling forms import in view works, Importing form code to view works too, but this is solution:
I added model = super().__class__()
in each init
function in model form class:
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
model = super().__class__() # 'soft code' prevents operational error
self.fields['name'].required = False
And also I need to modify checkbox list:
# CATEG_CHOICES= Category.choices_objects.all()
# CATEG_CHOICES=Category.objects.values_list('id','name') # without ..
choice_list = []
try:
choice_list.extend(Category.choices_objects.all())
# choice_list.extend(Category.objects.values_list('id','name'))
except OperationalError:
pass # happens when db doesn't exist yet
CATEG_CHOICES=choice_list
- [Django]-Validators = [MinValueValidator] does not work in Django
- [Django]-AngularJS with Django – Conflicting template tags
- [Django]-Django: Equivalent of "select [column name] from [tablename]"
-1👍
The only way to fix my error was commenting out every single file. This error might occur while a file thinks the database is not empty, or that it actually exists.
I just commented out the entire project, migrated, uncommented the models.py file, migrated again, uncommented the entire project. I have no idea why it worked.
- [Django]-No module named django but it is installed
- [Django]-Django / Comet (Push): Least of all evils?
- [Django]-Django Many-to-Many (m2m) Relation to same model