Python + Django 在使用migrate 的时候报错,有点坑

错误描述:django.db.migrations.exceptions.BadMigrationError: Migration test in app no Migration class

这个错报出之前其实还有一个坑,就是导入mysql 模块的问题.
ImportError: No module named MySQLdb
在Ubuntu 环境下 安装mysql 依赖库的方法是:

apt-get install python-mysqldb```
网上有很多说明,但是注意自己的系统版本

好,接下来说这个No migration class 的问题
说之前先总结下使用migration 迁移策略的顺序.

第一步:创建数据库 
第二步:在项目下的setting.py中配置数据库的相关设置.下面是我实测后的一个范例.

DATABASES = {
‘default’: {
‘ENGINE’: ‘django.db.backends.mysql’,
‘NAME’: ‘UbuntuTest’,
‘USER’: ‘root’,
‘PASSWORD’:’090201007′,
‘HOST’:’192.168.1.99′,
‘PORT’:’3306′,
}
}

第三步:是在自己的app项目中的models创建模型,下面是我创建模型的实测范例.

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默认安装的app,
yourappName,
]“`

第五步开始执行命令:

python manager.py makemigrations 

这个时候执行 这个命令 可能 会报上面提到的

django.db.migrations.exceptions.BadMigrationError: Migration test in app no Migration class

错误,因为我就遇到了.
参考了这个链接 删除自己app下migration 目录下的所有文件.
再运行 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

我这就不报错了 在migration目录下创建了一个0001_initial.py的文件.创建成功.
接着第六步,运行下第二个命令

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

在数据库中查看创建表成功.

补充关于migration 迁移的两个摘抄的说明:

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

两个不是翻译的关系 😊不同地方摘抄的.

谈谈机制:migrations机制有两个指令,第一个是makemigrations,第二个是migrate,生成migrations代码的makemigrations指令是用models里面的model和当前的migrations代码里面的model做对比,如果有新的修改,就生成新的migrations代码,migrate指令是用migrations目录中代码文件和django数据库djaong_migrations表中的代码文件做对比,如果表中没有,那就对这些没有的文件按顺序及依赖关系做migrate apply,然后再把代码文件名加进migrations表中。

    原文作者:机器人小雪
    原文地址: https://www.jianshu.com/p/17c9586f6d66
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞