Reputation: 829
I would like to use SQLAlchemy ORM not only for querying a database, but also to create the tables. So I connect to the empty schema of my database:
engine = create_engine(connection_string)
Base = declarative_base()
I have a few classes which looks 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 fine - the FIRST time. If I now drop the tables manually from the database OR calling Base.metadata.drop_all()
and restart the script (hence call create_all
again) I get an error that my tables already exists:
sqlalchemy.exc.OperationalError: (OperationalError) (1050, "Table 'someclass' already exists")
I can clearly say, that this is not true. I am able to 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, I will be able to create the tables again using create_all
.
So I guess this has something to do with the connection between SQLAlchemy and the database. Obviosly SQLAlchemy thinks the table still exists after I dropped it, which is not true. But if I try to query this none existing table it will tell me, that the table does not exist.
I googled around but wasn't able to find an explanation for this behavior. I do not use SQLAlchemy too often, so maybe I'm simply not deep enough into it, to understand the problem.
Anyway I would be thankful to get a hint.
I'm using Python 2.7.3 and MySQL 5.1.47 and SQLAlchemy 0.8.0b2
PS: Using Postgres I've never suffered from such an issue.
Upvotes: 3
Views: 2477
Reputation: 829
I've fixed the issue by upgrading to MySQL 5.1.66. Note that I did not just upgrade the old instance, but unistalled it completely from my system and installing the new version from scratch. Thus I am still not able to tell if the issue was due to a misconfiguration of my old installation or just a bug.
However, now everything works fine.
Upvotes: 1