flask-migrate出错(MySQLdb._exceptions.OperationalError) (1050, “Table ‘xxxx’ already exists”)
2020-06-23 16:08
1241 查看
flask数据库(mysql)迁移migrate出现错误
sqlalchemy.exc.OperationalError: (MySQLdb._exceptions.OperationalError) (1050, “Table ‘xxxx’ already exists”)
问题
修改了模型类中的某一个字段
#更改前name = db.Column(db.String(28), nullable=False) name = db.Column(db.String(36), nullable=False)
执行python database.py db upgrade的时候,出现sqlalchemy.exc.OperationalError: (MySQLdb._exceptions.OperationalError) (1050, “Table ‘xxxx’ already exists”)的报错
migrate 总是检测我的操作是重新建立我这个字段所在的表而不是 修改字段。
解决
打开migrations\versions下对应生成的迁移文件,发现自己的修改与migrate生成的迁移命令不同
其实迁移文件中有句话:# ###Alembic自动生成命令,请调整!###
upgrade中的内容和我的修改内容不一致,只能手动修改
这里以改变字段name的长度为例:
删除原来的
op.create_table('xxxx', sa.Column('create_time', sa.DateTime(), nullable=True), sa.Column('update_time', sa.DateTime(), nullable=True), sa.Column('id', sa.Integer(), nullable=False), ...... op.drop_table('xxxx')
添加上
from sqlalchemy.dialects import mysql
op.alter_column('ib_admin', 'name', existing_type=mysql.VARCHAR(length=36), nullable=False)
即:
总结
自动创建的迁移脚本会根据模型定义和数据库当前状态的差异,生成upgrade()和downgrade()函数的内容。对比不一定完全正确,有可能会遗漏一些细节,需要进行检查。
发现外键的似乎对比不出来。即模型类的外键部分没有变动,生成迁移文件的时候,还是会生成关于外键变化的迁移内容。
发现迁移文件有误后,其他的更改参考官方文档
migrate迁移文件的更改命令
相关文章推荐
- django 由inspectdb引起的django.db.utils.InternalError: (1050, "Table 'xxxx' already exists")
- sqlalchemy.exc.OperationalError: (MySQLdb._exceptions.OperationalError)(1050, "Table 'articles' alr
- 数据迁移问题(django.db.utils.InternalError:(1050,"Table 'tb_content' already exists"))
- django.db.utils.OperationalError: (1050, "Table 'devtypeinfo' already exists
- magento -- 解决magento错误:ERROR: Base table or view already exists: 1050 Table ... already exists
- MySQL出现[Err] 1050 - Table 't_active' already exists异常
- 错误代码: 1050 Table 'emp' already exists
- 错误:django.db.utils.InternalError: (1050, "Table 'tb_books' already exists")
- python 迁移文件 1050, "Table 'django_content_type' already exists"
- Django连接MySQL数据库时出错:django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No mo
- Laravel Base table or view already exists: 1050 Table 'users' already exists
- django migrate error: table 'xxx' already exists
- magento -- magento错误:SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'core_resource' already exists
- 1050, "Table 'django_content_type' already exists"
- flask安装mysql-python出错(环境ubuntu14.04)
- 在sqlplus中set autotrace on 出错的解决办法( Unable to verify PLAN_TABLE)
- Flask-Migrate使用
- MySQL: "Table 'xx.XXXX' doesn't exist"
- WebService生成XML文档时出错。不应是类型XXXX。使用XmlInclude或SoapInclude属性静态指定非已知的类型。