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
1
  • 1
    Check out Flask-Migrate, by the author of the Flask Mega Tutorial. Commented Oct 14, 2014 at 0:49

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Ahh good to know. I ended up just using Flask-Migrate as dirn suggested.
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"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.