6

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.

1
  • You have module level code which is trying to fetch objects from the database when the module loads before the table has been created. Here's a similar question. The full traceback will show you where the lookups are occurring.
    – Alasdair
    Oct 9, 2017 at 8:43

3 Answers 3

5

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(...)).

3
  • 1
    Thank you, this helped me fix my issue. As soon as I commented out my forms I was able to makemigrations and migrate
    – echodrome
    Jun 5, 2020 at 17:44
  • is it possible to have a fix for this without commenting the line out? Nov 11, 2020 at 12:58
  • I'm afraid not. If you understood how django works, you wouldn't be asking that :)
    – Eric
    Dec 17, 2020 at 14:22
4

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.

2
  • do I need to remove __init__.py file in Migrations folder?
    – Nairum
    Jul 30, 2019 at 8:33
  • @AlexeiMarinichenko No.
    – shacker
    Aug 13, 2019 at 7:08
3

Review, if you have any dependencies, is possible same Model need the Model Country in the same app or other app like:

class OtherModel(models.Model):
    country = models.ForeignKey(Country)

1.- If is True, you need to review if installed_apps in settings.py have the correct order of apps, if is in the same app, you need to declare first a Country app and then the dependents.

2.- If dependent is in the same app, the dependent Model need to be declared after Country model in models.py.

3.- Review if the error track on console talk about same erros on models.py or forms.py

4.- Review if when executing makemigrations and migrate is the correct order of apps: python manage.py makemirgations app_of_country, other_app_name

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.