pythonflasksqlalchemyflask-migratesqlalchemy-migrate

SQLAlchemy migration table already exist


I just got started using flask-migrate and I encounter some problems with it. In order to get myself familiarized with it, I started a new project with a mock MySQL database.

I run the migration as following

 export FLASK_APP=run.py
 flask db init
 flask db migrate
 flask db upgrade

and it throws this error:

   ...
  File "c:\users\terkea\appdata\local\programs\python\python36\lib\site-packages\MySQLdb\connections.py", line 226, in query
    _mysql.connection.query(self, query)
sqlalchemy.exc.OperationalError: (MySQLdb._exceptions.OperationalError) (1050, "Table 'user' already exists")
[SQL:
CREATE TABLE `User` (
        id INTEGER NOT NULL AUTO_INCREMENT,
        public_id VARCHAR(50),
        name VARCHAR(50),
        password VARCHAR(255),
        PRIMARY KEY (id),
        UNIQUE (name),
        UNIQUE (public_id)
)

]
(Background on this error at: http://sqlalche.me/e/e3q8)

Solution

  • The solution in my case was to

    1. get rid of __tablename__ from all my models
    2. Drop all the tables from the database before running the migration