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

12 Feb 2014 in TIL

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

bash
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:

python
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:

python
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:

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