简体   繁体   中英

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM