Liquibase设置布尔类型默认值遇到问题
Got it, let's figure out why you're hitting this error and fix it quickly. The problem here is that MySQL maps Liquibase's type="boolean" to BIT(1), and this type doesn't accept string values like 'false' as a default. Even though you set defaultValueBoolean="0", Liquibase translates that to the string literal which MySQL rejects for BIT columns. Here are a few solid fixes:
Option 1: Use defaultValueNumeric for BIT(1) columns
Since MySQL expects numeric values (0 or 1) for BIT(1) defaults, replace defaultValueBoolean with defaultValueNumeric:
<addColumn tableName="contact_client"> <column defaultValueNumeric="0" name="obsolete" type="boolean"/> </addColumn>
This will generate valid SQL: ALTER TABLE myApp.contact_client ADD obsolete BIT(1) DEFAULT 0 NULL which MySQL accepts without issues.
Option 2: Use BOOLEAN type instead (MySQL-friendly)
If you don't strictly need BIT(1), use type="BOOLEAN" (MySQL maps this to TINYINT(1) under the hood, which supports boolean literals). Then your original defaultValueBoolean will work:
<addColumn tableName="contact_client"> <column defaultValueBoolean="false" name="obsolete" type="BOOLEAN"/> </addColumn>
The generated SQL will be ALTER TABLE myApp.contact_client ADD obsolete TINYINT(1) DEFAULT false NULL, which is fully compatible with MySQL.
Option 3: Simplify with just defaultValue for BIT(1)
If you want to stick with BIT(1), you can drop the defaultValueBoolean attribute and just use defaultValue="0"—Liquibase will generate the correct numeric default:
<addColumn tableName="contact_client"> <column defaultValue="0" name="obsolete" type="boolean"/> </addColumn>
This avoids any conflict between the two default value attributes and produces valid SQL for the BIT type.
Why your original code failed
You had both defaultValue="0" and defaultValueBoolean="0" set. Liquibase prioritized the boolean attribute, translating it to the string 'false'—but MySQL's BIT(1) type doesn't recognize string literals as valid defaults, hence the error.
内容的提问来源于stack exchange,提问作者thomas

