postgresql liquibase 的枚举数据类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5133423/
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-09-20 00:35:17  来源:igfitidea点击:

enum data type for liquibase

postgresqlliquibaseliquid-layout

提问by Ali Taha Ali Mahboub

I'm currently working on a liquibase.xml file to create table table_a. One of my fields is <column name="state" type="ENUM('yes','no')">I'm using postgresql as my DBMS. is there anything like enum data type? I've read in this like http://wiki.postgresql.org/wiki/Enum

我目前正在使用 liquibase.xml 文件来创建表 table_a。我的领域之一是<column name="state" type="ENUM('yes','no')">我使用 postgresql 作为我的 DBMS。有没有类似 enum 数据类型的东西?我读过这样的http://wiki.postgresql.org/wiki/Enum

that postgresql doesn't have such data type. CREATE TYPE function is used to create this data type. I still don't know how to make it in liquibase though.

postgresql 没有这样的数据类型。CREATE TYPE 函数用于创建此数据类型。我仍然不知道如何在 liquibase 中制作它。

Any suggestions?

有什么建议?

回答by a_horse_with_no_name

Well of course PostgreSQL has an enum type (which is clearly documented in the link you have shown and the manual).

当然,PostgreSQL 有一个枚举类型(在您显示的链接和手册中清楚地记录了这一点)。

I don't think Liquibase "natively" supports enums for PostgreSQL, but you should be able to achieve it with a custom SQL:

我不认为 Liquibase “本机”支持 PostgreSQL 的枚举,但您应该能够使用自定义 SQL 实现它:

<changeSet id="1" author="Arthur">
  <sql>CREATE TYPE my_state AS ENUM ('yes','no')</sql>
  <table name="foo">
    <column name="state" type="my_state"/>
  </table>
</changeSet>

For a simple yes/no column, I'd actually use the booleantype instead of an enum

对于一个简单的是/否列,我实际上会使用boolean类型而不是枚举

回答by mu is too short

An alternative to creating a new type would be a simple CHECK constraint on a varchar(3)column:

创建新类型的替代方法是对varchar(3)列进行简单的 CHECK 约束:

<changeSet id="1" author="X">
    <table name="t">
        <column name="c" type="varchar(3)"/>
    </table>
    <sql>ALTER TABLE t ADD CONSTRAINT check_yes_no CHECK (c = 'yes' OR c = 'no')</sql>
</changeSet>

That might play better with the client side, or not. I think boolean(as suggested by a_horse_with_no_name) would be a better call for this specific case: saying exactly what you mean usually works out better than the alternatives.

这可能在客户端玩得更好,或者不玩。我认为boolean(如 a_horse_with_no_name 所建议的)对于这种特定情况会更好:准确说出你的意思通常比替代方案更有效。