简体   繁体   中英

How does one resolve a “Table already exists” error in Flask?

I'm having an issue I don't really know how to approach with Python Flask. For reference, I'm using SQL-Alchemy and flask-migrate for the database. That said. I'm getting the following error: "table guilds model already exists" when I run the command flask db upgrade. I have run this command immediately after creating a new migration with the command ' flask db migrate -m "Guilds Creation Migration" '. The flask upgrade command normally does exactly as it says. To quote the person who wrote flask-migrate "The flask DB migrate command does not make any changes to the database, it just generates the migration script. To apply the changes to the database, the flask DB upgrade command must be used." Inside the models.py file the code used to generate the table it is making reference to is the following:

class GuildModel(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    GuildName = db.Column(db.String(300), index=True, unique=True)
    GuildDescription = db.Column(db.String(64))

    def __repr__(self):
        return '<Guild {}>'.format(self.GuildName)

I have no real idea of how to resolve this problem or what is causing it. The searches I have done for a solution to this problem have not really returned much of anything. Any help would be very much appreciated

(Ps: The database I'm using is SQLite)

This issue seems to be resolvable in a number of ways. In my case, I had only created one singular database migration. The site this is for was in its testing phase as well only, so posts were totally unimportant. As was the entire database. So my solution was simple, delete the migrations folder and the database. This resolved the issue. I'm still not quite certain what caused the problem. However, it is very much resolved.

I was about to post similar query today! but some experiment got me the reason. Its because how either flask-sqlalchemy or flask-migrate works. its all because how WE (yes even i did similar mistake) define the class and table name.

as a solution you could use __tablename__ = GuildModel (even it will have same impact with no use) but even that will create issues as default nature of sqlalchemy and migrate woks, it converts the table name to guild_model

again on new migration it does not find GuildModel and creates new migration for it, but while upgrade it gives issues as here the conversion of class and default things encounters its existence.

SOLUTION

__tablename__ = guildmodel or __tablename__ = guild_model

use complete small case name. Do not use upper case. It did solved my issue, as i used the __tablename__ but the string has uppercase in between which created issues, getting rid of it got it working for me. Its all about how library works in its naming conventions.

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