Django : Table doesn't exist

82,061

Solution 1

  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

Solution 2

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

Solution 3

What solved my problem in situation when manage.py setmigration and then migrate to the SQL database is not working properly did is following:

  1. Run command : python manage.py migrate --fake MyApp zero
  2. After that: python manage.py migrate MyApp

And the problem that rises with connections.py and and after running correctly the migration command is solved! At least for me.

I'm using Python (3.x), MySQL DB, Django (3.x), and I was in situation when I needed after some time of successfully creating tables in my database, that some error regarding connections.py raises. But, above commands helped. I hope they will help all those who are having these type of problems.

Solution 4

I just ran migrations with the name of the app attached, for all the apps I had provisioned and that worked.

e.g. python3 manage.py makemigrations my_custom_app

After running for all of them I ran a migrate command to seal the deal. python3 manage.py migrate. That was it. I'm still wondering why django behaves this way sometimes though.

Solution 5

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

Share:
82,061
Wickkiey
Author by

Wickkiey

Updated on July 09, 2022

Comments

  • Wickkiey
    Wickkiey almost 2 years

    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 ?

  • maciek
    maciek over 7 years
    The problem is, with models commented out there are errors in rest of code…
  • Harsha Biyani
    Harsha Biyani about 7 years
    same with me.. if I comment out model, it is affecting in other code like in views..
  • doniyor
    doniyor about 7 years
    @HarshaBiyani of course, you need to turn off the imports for short time until you comment-in the models again
  • JDavies
    JDavies over 6 years
    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
  • Alex Daro
    Alex Daro about 6 years
    Thank you. But the real question still remains... Why does this happen?
  • doniyor
    doniyor about 6 years
    @AlexDaro there can be several reasons, but the most common one is that table gets removed manually in db level
  • Diesel
    Diesel over 5 years
    I'm really interested in why this happened. I created a new model, and in the migrate it just didn't make it. It did that in testing and development.
  • Nairum
    Nairum almost 5 years
    This solution does not work because there is User model and if I comment it, then Django fails. It is not possible to comment the whole project and the whole Django code.
  • Darwin
    Darwin over 2 years
    This only creates tables but with old fields and does not detect new fields.