python - Migrate Flask models.py to MySQL -
i'm new. bear me.
i'm developing flask application using sqlalchemy orm , until today have been using sqlite convenience. i'm putting application live on digital ocean , want use mysql instead of sqlite.
i have mysql installed on ubuntu vps on digital ocean , seems configured properly. however, have create database tables, can save data inside.
question: there way me migrate models.py, database tables created have written in models.py or have create database tables myself manually in mysql?
you can see application live here: http://workflowforum.dk/ , have made small test see if there database connection here: http://workflowforum.dk/testdb
models.py (only user model):
from flask import flask flask.ext.sqlalchemy import sqlalchemy flask.ext.script import manager flask.ext.migrate import migrate, migratecommand datetime import datetime, date hashlib import md5 bcrypt import hashpw, gensalt app = flask(__name__) app.config['sqlalchemy_database_uri'] = 'mysql://root:password@localhost/database' db = sqlalchemy(app) migrate = migrate(app, db) manager = manager(app) manager.add_command('db', migratecommand) class user(db.model): id = db.column(db.integer, primary_key=true) slug = db.column(db.string(80)) username = db.column(db.string(80), unique=true) email = db.column(db.string(80)) password = db.column(db.string(80), unique=false) admin = db.column(db.boolean(), default=false) join_date = db.column(db.datetime) last_seen = db.column(db.datetime) topics = db.relationship('topic') posts = db.relationship('post') picture = db.column(db.boolean(), default=false) title = db.column(db.string(80)) company = db.column(db.string(80)) summary = db.column(db.string(80)) class category(db.model): id = db.column(db.integer, primary_key=true) name = db.column(db.string(80), unique=true) description = db.column(db.string(180), unique=false) topics = db.relationship('topic', backref="category") class topic(db.model): id = db.column(db.integer, primary_key=true) slug = db.column(db.string(255), unique=true) title = db.column(db.string(80), unique=false) description = db.column(db.text, unique=false) pub_date = db.column(db.datetime) last_update = db.column(db.datetime) user_id = db.column(db.string(80), db.foreignkey('user.id')) category_id = db.column(db.integer, db.foreignkey('category.id')) views = db.column(db.integer, default=0) locked = db.column(db.boolean(), default=false) pinned = db.column(db.boolean(), default=false) user = db.relationship('user') posts = db.relationship('post')
views.py (only database test):
@app.route('/testdb') def testdb(): if db.session.query("1").from_statement("select 1").all(): return 'it works.' else: return 'something broken.'
update after lukas comment:
when trying db.create_all() traceback:
sqlalchemy.exc.operationalerror: (_mysql_exceptions.operationalerror) (1005, "can't create table 'pwforum.topic' (errno: 150)") [sql: u'\ncreate table topic (\n\tid integer not null auto_increment, \n\tslug varchar(255), \n\ttitle varchar(80), \n\tdescription text, \n\tpub_date datetime, \n\tlast_update datetime, \n\tuser_id varchar(80), \n\tcategory_id integer, \n\tviews integer, \n\tlocked bool, \n\tpinned bool, \n\tprimary key (id), \n\tunique (slug), \n\tforeign key(user_id) references user (id), \n\tforeign key(category_id) references category (id), \n\tcheck (locked in (0, 1)), \n\tcheck (pinned in (0, 1))\n)\n\n']
the db.create_all
suggestion in comments when don't use migration framework. looks using flask-migrate database migration framework here. problem doing create_all
migration scripts skipped, fixes need make convert sqlite models mysql not reflected in migration scripts.
my recommendation create empty mysql database, configure in flask app, , generate mysql tables calling:
$ ./manage.py db upgrade
when this, flask-migrate start running migration scripts 1 one , in order.
if failures, because of migration scripts have changes compatible sqlite not mysql. need fix these problems in migration scripts, until of them run cleanly, @ point have complete mysql database use in application.
Comments
Post a Comment