錯(cuò)誤描述:django.db.migrations.exceptions.BadMigrationError: Migration test in app no Migration class
這個(gè)錯(cuò)報(bào)出之前其實(shí)還有一個(gè)坑,就是導(dǎo)入mysql 模塊的問(wèn)題.
ImportError: No module named MySQLdb
在Ubuntu 環(huán)境下 安裝mysql 依賴(lài)庫(kù)的方法是:
apt-get install python-mysqldb```
網(wǎng)上有很多說(shuō)明,但是注意自己的系統(tǒng)版本
好,接下來(lái)說(shuō)這個(gè)No migration class 的問(wèn)題
說(shuō)之前先總結(jié)下使用migration 遷移策略的順序.
第一步:創(chuàng)建數(shù)據(jù)庫(kù)
第二步:在項(xiàng)目下的setting.py中配置數(shù)據(jù)庫(kù)的相關(guān)設(shè)置.下面是我實(shí)測(cè)后的一個(gè)范例.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'UbuntuTest',
'USER': 'root',
'PASSWORD':'090201007',
'HOST':'192.168.1.99',
'PORT':'3306',
}
}
第三步:是在自己的app項(xiàng)目中的models創(chuàng)建模型,下面是我創(chuàng)建模型的實(shí)測(cè)范例.
class UserInfo(models.Model):
username = models.CharField(max_length=50)
password = models.CharField(max_length=50)
class TestInfo(models.Model):
datetime = models.CharField(max_length=10)
第四步:將自己的app在setting中的 INSTALLED_APPS 中添加.
INSTALLED_APPS = [
.......django默認(rèn)安裝的app,
yourappName,
]```
第五步開(kāi)始執(zhí)行命令:
python manager.py makemigrations
這個(gè)時(shí)候執(zhí)行 這個(gè)命令 可能 會(huì)報(bào)上面提到的
django.db.migrations.exceptions.BadMigrationError: Migration test in app no Migration class
錯(cuò)誤,因?yàn)槲揖陀龅搅?
參考了這個(gè)鏈接 刪除自己app下migration 目錄下的所有文件.
再運(yùn)行 python manager.py makemigrations
eric@Eric:~/Eric/django/testDemo$ python manage.py makemigrations
Migrations for 'ericMigrations':
0001_initial.py:
- Create model TestInfo
- Create model UserInfo
我這就不報(bào)錯(cuò)了 在migration目錄下創(chuàng)建了一個(gè)0001_initial.py的文件.創(chuàng)建成功.
接著第六步,運(yùn)行下第二個(gè)命令
python manager.py migration
eric@Eric:~/Eric/django/testDemo$ python manage.py migrate
Operations to perform:
Apply all migrations: admin, ericMigrations, contenttypes, auth, sessions
Running migrations:
Rendering model states... DONE
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying ericMigrations.0001_initial... OK
Applying sessions.0001_initial... OK
在數(shù)據(jù)庫(kù)中查看創(chuàng)建表成功.
補(bǔ)充關(guān)于migration 遷移的兩個(gè)摘抄的說(shuō)明:
As the problem is related to the migration, you have to understand first how it works, django check you database schema compares it with your model then generates the migration script. Every migration script is executed one time, because django keep tracking you migrations. This is managed by a table called django_migrations that is created in your database the first time migrations are ran
兩個(gè)不是翻譯的關(guān)系 ??不同地方摘抄的.
談?wù)剻C(jī)制:migrations機(jī)制有兩個(gè)指令,第一個(gè)是makemigrations,第二個(gè)是migrate恋昼,生成migrations代碼的makemigrations指令是用models里面的model和當(dāng)前的migrations代碼里面的model做對(duì)比践啄,如果有新的修改,就生成新的migrations代碼肄扎,migrate指令是用migrations目錄中代碼文件和django數(shù)據(jù)庫(kù)djaong_migrations表中的代碼文件做對(duì)比,如果表中沒(méi)有,那就對(duì)這些沒(méi)有的文件按順序及依賴(lài)關(guān)系做migrate apply臣镣,然后再把代碼文件名加進(jìn)migrations表中。