简体   繁体   English

如何在Flask中将SQLAlchemy-Migrate与MySQL数据库一起使用

[英]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.

相关问题 将数据库迁移从sqlalchemy-migrate更改为Flask-Migrate - change database migrations from sqlalchemy-migrate to Flask-Migrate sqlalchemy-migrate DatabaseNotControlledError异常 - sqlalchemy-migrate DatabaseNotControlledError exception 如何在SQLAlchemy-Migrate中使用方言类型(HSTORE)创建迁移脚本? - How to create a migration script with dialect types (HSTORE) in SQLAlchemy-Migrate? 如何使用sqlalchemy-migrate编写alter column name migration? - How to write alter column name migrations with sqlalchemy-migrate? 如何使用sqlalchemy-migrate将列类型从字符更改为整数 - How to alter column type from character varying to integer using sqlalchemy-migrate 使用SQLAlchemy-Migrate创建具有外键约束的表时出错 - Error creating table with foreign key constraint using SQLAlchemy-Migrate Python无法在Docker映像中找到sqlalchemy-migrate - Python unable to find sqlalchemy-migrate in Docker image 在SQLAlchemy-migrate中,使用“测试”有什么意义? - In SQLAlchemy-migrate, what's the point of using “test”? pip install sqlalchemy-migrate崩溃并莫名其妙地燃烧 - pip install sqlalchemy-migrate crashes and burns inexplicably SQLAlchemy-Migrate 或 Alembic 在重命名 model 时删除数据 - SQLAlchemy-Migrate or Alembic deletes data when renaming model
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM