[英]How to use SQLAlchemy-Migrate with MySQL Database in Flask
I've been working on replacing my SQLite database with a MySQL database in my Flask app. 我一直在用Flask应用中的MySQL数据库替换SQLite数据库。
Previously I've been using scripts from the Mega Flask Tutorial to manage my database creation and migration using SQLAlchemy-Migrate. 以前,我一直在使用Mega Flask教程中的脚本来管理我使用SQLAlchemy-Migrate进行的数据库创建和迁移。
It seems that these scripts are not compatible with MySQL out of the box, and I can't really find anything on how to use SQLAlchemy-Migrate with MySQL. 这些脚本似乎与MySQL不兼容,而且我真的找不到有关如何在MySQL中使用SQLAlchemy-Migrate的任何信息。
How do you guys typically handle changes to your models and database migrations when in development with MySQL? 在使用MySQL开发时,你们通常如何处理模型更改和数据库迁移?
#Config
SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://root:tehnoose@localhost/app'
SQLALCHEMY_MIGRATE_REPO = os.path.join(basedir, 'db_repository')
#db_create.py
from migrate.versioning import api
from config import SQLALCHEMY_DATABASE_URI
from config import SQLALCHEMY_MIGRATE_REPO
from app import db
import os.path
db.create_all()
if not os.path.exists(SQLALCHEMY_MIGRATE_REPO):
api.create(SQLALCHEMY_MIGRATE_REPO, 'database repository')
api.version_control(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)
else:
api.version_control(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO, api.version(SQLALCHEMY_MIGRATE_REPO))
#Traceback
Traceback (most recent call last):
File "db_create.py", line 12, in <module>
api.version_control(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO, api.ve
rsion(SQLALCHEMY_MIGRATE_REPO))
File "C:\pyprojects\cc\flask\lib\site-packages\migrate\versioning\api.py", lin
e 133, in version
repo = Repository(repository)
File "C:\pyprojects\cc\flask\lib\site-packages\migrate\versioning\repository.p
y", line 77, in __init__
self.verify(path)
File "C:\pyprojects\cc\flask\lib\site-packages\migrate\versioning\repository.p
y", line 98, in verify
raise exceptions.InvalidRepositoryError(path)
migrate.exceptions.InvalidRepositoryError: C:\pyprojects\cc\db_repository
I got exactly the same situation today, finally I found they're compatible perfectly. 今天我的情况完全一样,最后我发现它们完全兼容。 Just create a new database in mysql and reset SQLALCHEMY_DATABASE_URI before you run the scripts.
在运行脚本之前,只需在mysql中创建一个新数据库并重置SQLALCHEMY_DATABASE_URI。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.