Django : Table doesn't exist

django migrate doesn't create table
django test table doesn't exist
django migrate not creating tables
django no such table
django delete table
django reset database
django recreate dropped table
django_session' doesn't exist

I dropped some table related to an app. and again tried the syncdb command

python manage.py syncdb

It shows error like

django.db.utils.ProgrammingError: (1146, "Table 'someapp.feed' doesn't exist")

models.py

class feed(models.Model):
    user = models.ForeignKey(User,null=True,blank=True)
    feed_text = models.CharField(max_length=2000)
    date = models.CharField(max_length=30)
    upvote = models.IntegerField(default=0)
    downvote = models.IntegerField(default=0)

    def __str__(self):
        return feed.content

What I can do to get the tables for that app ?

  1. drop tables (you already did),
  2. comment-out the model in model.py,
  3. and ..

if django version >= 1.7:

python manage.py makemigrations
python manage.py migrate --fake

else

python manage.py schemamigration someapp --auto
python manage.py migrate someapp --fake
  1. comment-in your model in models.py
  2. go to step 3. BUT this time without --fake

Django : Table doesn't exist, python manage.py syncdb. It shows error like django.db.utils.ProgrammingError: (1146, "Table 'someapp.feed' doesn't exist"). models.py class feed(models. It's accidental to delete a table when performing some actions or on purpose to get rid of cluster. Django has a good in-build engine that can manage the changes in models and your database synced but if you happen to drop a migrated table you will end up with Django : Table doesn't exist. Here's how you can fix it.

For those that may still be having trouble (like me), try this out:

Comment out all the URL's in the main app's urls.py

Then go ahead and run migrations:

$ ./manage.py makemigrations
$ ./manage.py migrate

The problem was alleviated by removing the ()'s

    solved_time = models.DateTimeField('solved time', default=timezone.now())

to

    solved_time = models.DateTimeField('solved time', default=timezone.now)

I got this answer from reddit

Recover dropped table in Django : Table doesn't exist, a quick fix for Django : Table doesn't exist error. Advert. It's accidental to delete a table when performing some actions or  Django : Table doesn't exist. 23. 9. I dropped some table related to an app. and again tried the syncdb command. python manage.py syncdb. if django version >= 1.7:

none of the above solutions worked for me, I finally solved by

sudo systemctl stop mysql.service

sudo apt-get purge mysql-server

sudo apt-get install mysql-server

sudo systemctl stop mysql.service

In my case the code that I pulled had managed = False and I wanted the tables to be maintained by Django.

But when I did makemigrations the custom tables were not being detected or I was getting the error that the app_name.Table_name does not exist

I tried the following:

  1. delete all the migration files inside the migrations folder (except init.py file) and then makemigrations then finally migrate
  2. above 2 answers
  3. this

PS: This solution is only feasible if backup is present or data is not important or you are just started creating the tables, as purging mysql will lead to loss of data

Migration error for multiple databases: django.db.utils , ProblemI dropped some table related to an app. and again tried this command 1$ python manage.py syncdb It shows error like 1django.db.utils  Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Learn more 'Table doesn't exist' on django makemigrations

I had this issue where I was playing with same database structure in production vs development. While dropping and recreating tables will probably resolve the issue, its worth checking your database itself and see if the model is actually correct. For myself I created the development database incorrectly with the table names all in lowercase while in production the first letter of tables were capitalized. I used the python manage.py inspectdb command on production db, and compared it to the model and realized that in the model it was trying to insert data to table 'test' instead of 'Test' for example. Hope that helps some of you in future.

Writing your first Django app, part 2 | Django documentation, Some of these applications make use of at least one database table, though, so The sqlmigrate command doesn't actually run the migration on your database  All management commands say 'Table myapp.myapp_mymodel doesn't exist'.. well yeah. The problem was a Form's forms.ModelChoiceField (queryset=MyModel.staticFunction () running on module load. Wrapping the static call with lazy () solved it. – jozxyqk Aug 3 '17 at 22:28

I have to face same issue and there are a couple of approaches, but the one I think is the most probable one.

Maybe you are loading views or queries to database but you haven´t granted enough time for Django to migrate the models to DB. That's why the "table doesn't exist".

Make sure you use this sort of initialization in you view's code:

Class RegisterForm(forms.Form):

  def __init__(self, *args, **kwargs):
    super(RegisterForm, self).__init__(*args, **kwargs)

A second approach is you clean previous migrations, delete the database and start over the migration process.

Migration changing db_table and managed not creating new table, Migration changing db_table and managed not creating new table any model marked as "managed=False" should never be changed with django's migration? The contenttypes error is probably triggered because the table doesn't exist. Defaults to True, meaning Django will create the appropriate database tables in migrate or as part of migrations and remove them as part of a flush management command. That is, Django manages the database tables’ lifecycles. If False, no database table creation or deletion operations will be performed for this model.

Integrating Django with a legacy database, If you do want to allow Django to manage the table's lifecycle, you'll need to change the managed option above to  RequestConfig ¶ class django_tables2.config.RequestConfig (request, paginate=True) [source] ¶. A configurator that uses request data to setup a table. A single RequestConfig can be used for multiple tables in one view.

Automatic migrations prevent creation of initial database table layout, site-packages\django\core\management\__init__.py", line 330, in execute There doesn't seem to be a way to create those tables in a way that doesn't result in  Models¶ A model is the single, definitive source of information about your data. It contains the essential fields and behaviors of the data you’re storing. Generally, each model maps to a single database table. The basics: Each model is a Python class that subclasses django.db.models.Model. Each attribute of the model represents a database

Digging Deeper Into Django Migrations – Real Python, Django uses a database table called django_migrations . Django doesn't strictly represent the order in which migrations are applied. If your app already has models and database tables, and doesn’t have migrations yet (for example, you created it against a previous Django version), you’ll need to convert it to use migrations by running:

Comments
  • The problem is, with models commented out there are errors in rest of code…
  • @HarshaBiyani of course, you need to turn off the imports for short time until you comment-in the models again
  • THANK YOU!!! I had an issue where it wasn't adding the table to my DB in production. Spent hours trying to fix it by removing the migrations. However, just faking the migrations and re-adding the model fixed the issue. Django 1.10
  • Thank you. But the real question still remains... Why does this happen?
  • @AlexDaro there can be several reasons, but the most common one is that table gets removed manually in db level