java Liquibase 插入 BIT 列,MySQL,列的数据太长

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31252711/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 18:20:56  来源:igfitidea点击:

Liquibase inserting into BIT column, MySQL, data too long for column

javaliquibase

提问by dustin.schultz

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

在 Liquibase 中,我定义了一个包含类型为 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'

在随后的变更集中,我想将数据插入到该表中,但是,当将数据插入 BIT(1) 类型的“活动”列时,MySQL 会抱怨“数据截断:列的数据太长”

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?

插入 BIT(1) 列的正确方法是什么?

回答by dustin.schultz

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

回答我自己的问题,因为我在发布后立即想到了这一点。要插入 BIT(1) 列,您需要将值定义为valueBoolean

<insert>
   <column name="active" valueBoolean="true"/>
</insert>

回答by user436357

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:

有从CSV文件加载每个表中的记录与当类似的情况<loadData>。在<loadData>元素中,您必须为表中的每个布尔列明确指定类型:

<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.

希望它可以帮助到这里遇到麻烦的其他人。

回答by Christophe Moine

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

就我而言,我使用的是 loadData 而不是插入,我不得不使用以下内容:

<column name="active" type="boolean"/>

回答by Hubert Schumacher

In my case with MariaDB it had to be:

在我使用 MariaDB 的情况下,它必须是:

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

按照dustin.schultz 的建议省略'type="bit"' 我得到一个Liquibase 验证错误:

column 'type' is required for all columns