简体   繁体   English

Django makemigrations上的“表不存在”

[英]'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: 在使用mysqldjango 1.11应用程序上,我有3个应用程序,其中一个是“国家”模型:

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: 当我尝试进行makemigrations此错误:

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. 如果我使用./manage.py makemigrations another_app为与该模型及其数据库表无关的另一个应用程序进行迁移,则仍然会出现此错误。

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. 1.-如果为True,则需要检查settings.py中的installed_apps是否具有正确的应用顺序,如果位于同一应用中,则需要先声明一个Country应用,然后再声明其从属产品。

2.- If dependent is in the same app, the dependent Model need to be declared after Country model in models.py. 2.-如果依赖项在同一应用程序中,则需要在models.py中的“国家/地区”模型之后声明依赖项模型。

3.- Review if the error track on console talk about same erros on models.py or forms.py 3.-检查控制台上的错误轨道是否谈论了models.py或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 4.-检查执行makemigrations和迁移时是否是应用程序的正确顺序:python manage.py makemirgations app_of_country,other_app_name

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. Django以某种方式认为您已经创建了该表,现在正在尝试对其进行修改,而实际上您已经从外部删除了该表并重新开始。 If that's the case, delete all of the files in migrations folders belong to your apps and start over with ./manage.py makemigrations . 如果是这种情况,请删除migrations文件夹中属于您应用的所有文件,然后从./manage.py makemigrations重新开始。

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. 简而言之,我有一些类似forms.ChoiceField(choices=get_some_data(),...) ,其中get_some_data()使用数据库来检索一些默认值。

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(...) ). 我希望您已经发布了回溯,因为在我的情况下,通过查看回溯, get_some_data()正在使用orm(使用诸如somemodel.objetcs.filter(...)类的回溯get_some_data()是很明显的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM