SQLite compiles false() incorrectly (in server_default)

Issue #2387 resolved
Wichert Akkerman created an issue
import sqlalchemy
from sqlalchemy import schema
from sqlalchemy import types
from sqlalchemy.sql.expression import false
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base

metadata = schema.MetaData()
BaseObject = declarative_base(metadata=metadata)

class Article(BaseObject):
    __tablename__ = "article"

    id = schema.Column(types.Integer(), primary_key=True)
    seen = schema.Column(types.Boolean(),
            server_default=false(), nullable=False)

engine = sqlalchemy.create_engine('sqlite:///:memory:')
metadata.create_all(engine)
Session = sessionmaker(bind=engine)

session = Session()
session.add(Article())
session.flush()

gives this error:

sqlalchemy.exc.IntegrityError: (IntegrityError) constraint failed u'INSERT INTO article DEFAULT VALUES' ()

the generated SQL gives a hint what is happening:

CREATE TABLE article (
    id INTEGER NOT NULL, 
    seen BOOLEAN DEFAULT false NOT NULL, 
    PRIMARY KEY (id), 
    CHECK (seen IN (0, 1))
)

It looks like {{{false()}}} is compiled to {{{false}}}, while the CHECK condition only accepts 0 and 1 as values.

Comments (1)

  1. Mike Bayer repo owner

    It's so very strange when some issue that has been around for many years is reported identically, almost line for line, twice within two weeks. This was just reported/fixed in #2368 and the test above runs fine in tip.

  2. Log in to comment