24

In Liquibase, I define a table with a column of type BIT(1)

<changeSet author="foobar" id="create-configuration-table">
    <createTable tableName="configuration">
        <column autoIncrement="true" name="id" type="BIGINT(19)">
            <constraints primaryKey="true" />
        </column>
        <column name="active" type="BIT(1)" />
        <column name="version" type="INT(10)" />
    </createTable>
</changeSet>

In the subsequent changeset, I want to insert data into this table, however, when inserting data into the 'active' column of type BIT(1), MySQL complains 'Data truncation: Data too long for column'

I have tried:

<insert>
   <column name="active" value="1" type="BIT(1)" />
</insert>

and

<insert>
   <column name="active" value="1"/>
</insert>

and

<insert>
   <column name="active" value="TRUE" type="BOOLEAN"/>
</insert>

What is the correct way to insert into a BIT(1) column?

dustin.schultz
  • 13,076
  • 7
  • 54
  • 63

4 Answers4

45

Answering my own question as I figured this out right after I posted it. To insert into a BIT(1) column, you need to define the value as valueBoolean

<insert>
   <column name="active" valueBoolean="true"/>
</insert>
dustin.schultz
  • 13,076
  • 7
  • 54
  • 63
  • I had similar problem with data load from CSV. If data is loaded from CSV node should declare field as , and values in CSV should be true/false – igor.beslic Apr 10 '18 at 08:39
13

There is a similar case when loading records per table from csv files with <loadData>. In the <loadData> element, you have to explicitly specify type for each Boolean columns in the table:

<loadData encoding="UTF-8"
          file="path/to/file.csv"
          separator=","
          tableName="MY_TABLE"
>
    <!-- specify that values in my_boolean_column should be interpreted as Boolean values -->
    <column name="my_boolean_column" type="BOOLEAN" />
</loadData>

Hope it helps other folks who landed here having trouble with this.

user436357
  • 363
  • 3
  • 9
3

In my case, I was using loadData instead of insert, and I had to use the following:

<column name="active" type="boolean"/>
Christophe Moine
  • 816
  • 1
  • 7
  • 7
  • To indicate that the value to be inserted is a bit (or multiple bits) Mysql and MariaDB use the notations: b'0' respectively b'1' or for multiple bits e.g. b'00100110' – Hubert Schumacher Feb 04 '17 at 06:07
0

In my case with MariaDB it had to be:

<column name="show_in_app_directory" type="bit" valueBoolean="true" />

Leaving out 'type="bit"' as suggested by dustin.schultz I get a Liquibase validation error:

column 'type' is required for all columns
dustin.schultz
  • 13,076
  • 7
  • 54
  • 63
Hubert Schumacher
  • 1,683
  • 1
  • 16
  • 25