3

I've been working on replacing my SQLite database with a MySQL database in my Flask app.

Previously I've been using scripts from the Mega Flask Tutorial to manage my database creation and migration using 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.

How do you guys typically handle changes to your models and database migrations when in development with 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
Chockomonkey
  • 3,895
  • 7
  • 38
  • 55
  • 1
    Check out [Flask-Migrate](http://flask-migrate.readthedocs.org/en/latest/), by the author of the Flask Mega Tutorial. – dirn Oct 14 '14 at 00:49

2 Answers2

4

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.

chris
  • 41
  • 2
0

Yes, you can use Maria , MySql , sql , etc db with SqlAlchemy to Migrate tables. Just need to make sure the URI you are passing should be proper for Particular database use below code to do so

SQLALCHEMY_DATABASE_URI = "mysql+pymysql://username:password@localhost/DatabaseName"
FaizGeeky
  • 39
  • 3