alembic - TypeError: Boolean value of this clause is not defined

When trying to add columns to a table using alembic, I ran into the following error:

File "/path/to/site-packages/sqlalchemy/sql/elements.py", line 460, in __bool__
    raise TypeError("Boolean value of this clause is not defined")

Here’s the code that I was using:

def upgrade():
    op.add_column('table',
        sa.Column('field_one', sa.String(50), nullable=False),
        sa.Column('field_two', sa.String(50), nullable=True)
    )

I’d copied and pasted it from somewhere online and just added the second column as I wanted to add multiple columns. This is what broke SQLAlchemy.

Unfortunately, the error message isn’t very descriptive. It sent me on a wild goose chase through the SQLAlchemy docs before I finally decided just to delete code until it worked. After realising that the second item was causing issues, I tried using op.add_columns rather than op.add_column but was greeted by the following error:

AttributeError: 'module' object has no attribute 'add_columns'

To get it working, you have to add the columns one at a time:

def upgrade():
    op.add_column('table', sa.Column('field_one', sa.String(50), nullable=False))
    op.add_column('table', sa.Column('field_two', sa.String(50), nullable=True))

It’s worth noting that the same applies to dropping columns too:

def downgrade():
        op.drop_column("table", "field_one")
        op.drop_column("table", "field_two")

Published:
February 12, 2014

Category
Development