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

在使用Python Django进行数据库迁移时遇到错误:BadMigrationError,提示Migration test in app 无Migration类。之前还遇到ImportError: No module named MySQLdb。在Ubuntu环境下,通过apt-get install python-mysqldb解决MySQL模块导入问题。迁移步骤包括:配置数据库、创建模型、添加app到INSTALLED_APPS、执行makemigrations和migrate命令。遇到BadMigrationError时,删除app下migration目录下的所有文件,重新运行makemigrations即可解决问题。Django的migrations机制通过makemigrations和migrate两个指令来管理数据库迁移,确保模型变化与数据库同步。
摘要由CSDN通过智能技术生成

错误描述: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表中。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值