Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'Table doesn't exist' on django makemigrations

On a django 1.11 application which uses mysql , I have 3 apps and in one of them I have a 'Country' model:

class Country(models.Model):
    countryId = models.AutoField(primary_key=True, db_column='country_id')
    name = models.CharField(max_length=100)
    code = models.CharField(max_length=3)

    class Meta:
        db_table = 'country'

Whaen I try to makemigrations I get this error:

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

If I run making migration for another app which is not related to this model and its database table using ./manage.py makemigrations another_app, I still get this error.

like image 693
s_puria Avatar asked Oct 09 '17 07:10

s_puria


People also ask

What does Makemigrations do in Django?

makemigrations is responsible for packaging up your model changes into individual migration files - analogous to commits - and migrate is responsible for applying those to your database.

What is manage py Makemigrations?

makemigrations basically generates the SQL commands for preinstalled apps (which can be viewed in installed apps in settings.py) and your newly created apps' model which you add in installed apps. It does not execute those commands in your database file.


2 Answers

I've had this problem and it's because I was initializing a default value somewhere in a model using... the database that I had just dropped. In a nutshell I had something like forms.ChoiceField(choices=get_some_data(),...) where get_some_data() used the database to retrieve some default values.

I wish you had posted the backtrace because in my case it's pretty obvious by looking at the backtrace that get_some_data() was using the orm (using something like somemodel.objetcs.filter(...)).

like image 98
Eric Avatar answered Oct 04 '22 00:10

Eric


Somehow, Django thinks you've already created this table and are now trying to modify it, while in fact you've externally dropped the table and started over. If that's the case, delete all of the files in migrations folders belong to your apps and start over with ./manage.py makemigrations.

like image 30
shacker Avatar answered Oct 03 '22 23:10

shacker