Blubber
Blubber

Reputation: 2254

Enums in SQLite

My model uses a couple of Enum's for various columns, creating the tables using SQLAlchemy's create_all() method works fine in PostgreSQL, but it doesn't work with SQLite, it just stalls.

The problem seems to be with creating Enum's, as far as I can tell sqlite doesn't support these, but according to SQLAlchemy's docs that shouldn't pose a problem. When I try to create_all() on an sqlite memory db it just stalls, even with echo=True no output appears.

I tried the following code to demonstrate the problem:

from sqlalchemy import create_engine, Enum
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()
e = Enum('foo', 'bar', metadata=Base.metadata)

engine = create_engine('sqlite:///:memory:', echo=True)
Base.metadata.create_all(bind=engine)

When I run this script it shows no output whatsoever, it just stalls. Python uses 100% CPU and the script never ends, until I ctrl-c it.

When I try to create_all() on my actual schema it does echo some PRAGMA commands trying to determine if tables exist, but then it stalls on the creating the Enums. I tried to remove code from the model definition until it worked just fine, this was when I figured out it's the Enums.

I tried to run this on Python 3.4 with SQLAlchemy 0.9.6 using SQLite 3.7.13.

Upvotes: 1

Views: 1129

Answers (1)

Eevee
Eevee

Reputation: 48546

A friend ran into exactly this same problem recently, and it looks to me like an infinite-loop bug in SQLA (which I should really report, so thanks for this minimal testcase :)).

Just remove the metadata= kwarg from your real code; as long as the enum is used as a type inside a declarative class, it'll inherit the right metadata anyway.

Upvotes: 2

Related Questions