I am running an application with Flask and Angular hosted on heroku. I have a problem with migrating heroku postgresql. I use flask-migrate , which is a tiny wrapper around alembic. Locally, everything is in order. I got an exception when I run heroku run upgrade , which runs the alembic update command.
INFO [alembic.migration] Context impl PostgresqlImpl. INFO [alembic.migration] Will assume transactional DDL. INFO [alembic.migration] Running upgrade None -> 19aeffe4063d, empty message Traceback (most recent call last): File "manage.py", line 13, in <module> manager.run() ... cursor.execute(statement, parameters) sqlalchemy.exc.ProgrammingError: (ProgrammingError) relation "users" already exists '\nCREATE TABLE users (\n\tid SERIAL NOT NULL, \n\tusername VARCHAR(32), \n\tpassword_hash VARCHAR(128), \n\tPRIMARY KEY (id)\n)\n\n' {}
Simple, alembic tries to start from the first migration, which creates db. I tried to explicitly install the correct version with heroku run python manage.py db upgrade +2 or revision number, but the exception is the same.
lukas$ heroku run python manage.py db current Running `python manage.py db current` attached to terminal... up, run.1401 INFO [alembic.migration] Context impl PostgresqlImpl. INFO [alembic.migration] Will assume transactional DDL. Current revision for postgres:
My guess was that due to the ephemeral file system, Heroku's revisions were not saved, but that should not be a problem if I explicitly install the revision, right? :)
How to set current version header?
Here is the relevant code:
PROCFILE:
web: gunicorn server:app init: python manage.py db init upgrade: python manage.py db upgrade
models.py
db = SQLAlchemy() ROLE_USER = 0 ROLE_ADMIN = 1 class User(db.Model): __tablename__ = "users" id = db.Column(db.Integer, primary_key = True) username = db.Column(db.String(32), unique = True, index = True) email = db.Column(db.String(255), unique = True) password_hash = db.Column(db.String(128)) role = db.Column(db.SmallInteger, default = ROLE_USER)