SQLAlchemy says "The table already exists" even though it is not
I would like to use SQLAlchemy ORM not only to query the database but also to create tables. So I am connecting to an empty schema in my database:
engine = create_engine(connection_string)
Base = declarative_base()
I have several classes that are similar to the following example:
class SomeClass(Base):
__tablename__= 'SomeClass'
id = Column(Integer, primary_key=True, auto_increment=True)
name = Column(String(50))
def __init__(self, name):
self.name = name
To create the database, I call:
Base.metadata.create_all(engine)
This works great - FIRST time. If I now delete the tables manually from the database OR by calling Base.metadata.drop_all()
and restarting the script (hence calling create_all
again), I get an error that my tables already exist:
sqlalchemy.exc.OperationalError: (OperationalError) (1050, "Table 'someclass' already exists")
I can clearly say that it is not. I can create a table with the same name using CREATE TABLE from my CLI tool and also, if I now stop the database daemon and start it again, Iwill be able to create tables again using create_all
.
So, I think it has something to do with the connection between SQLAlchemy and the database. Obviosly SQLAlchemy thinks the table still exists after I dumped it, which is not correct. But if I try to query this none existing table, it tells me that the table does not exist.
I googled around but couldn't find an explanation for this behavior. I use SQLAlchemy too often, so maybe I'm just not deep enough in this to understand the problem.
Anyway, I'd appreciate a hint.
I am using Python 2.7.3 and MySQL 5.1.47 and SQLAlchemy 0.8.0b2
PS: Using Postgres I have never suffered from such a problem.
source to share
I fixed the problem by upgrading to MySQL 5.1.66. Please note, I didn't just update the old instance, but completely disconnected it from my system and installed the new version from scratch. So I still can't tell if the problem was due to a misconfiguration of my old installation or just a bug.
However, everything is working fine now.
source to share