如何在 PostgreSQL 中插入具有默认值的枚举字段?

huangapple go评论117阅读模式
英文:

How to insert a enum field with default value postgresql?

问题

这里我需要为我的枚举类型设置默认值,以防出现空值。

默认情况下,它不起作用。我在创建表时设置了默认值。

如果我在值中传递默认值,那么默认值将出现,但是我既有空数据又有枚举数据作为输入。

目前我是这样传递的:

  1. INSERT INTO table(enum_value_1, enum_value_2)
  2. VALUES (NULL, 'enum_value')

但是我希望数据如下所示:

  1. INSERT INTO table(enum_value_1, enum_value_2)
  2. VALUES ('enum_value_default', 'enum_value')

创建表的命令如下:

  1. CREATE TYPE enum_candidate_status AS ENUM('attended', 'selected', 'rejected', 'not attended');
  2. CREATE TYPE enum_rating AS ENUM('1', '2', '3', '4', '5');
  3. CREATE TABLE CANDIDATE (
  4. candidate_id SERIAL PRIMARY KEY,
  5. first_name varchar(100),
  6. candidate_status enum_candidate_status DEFAULT 'not attended',
  7. interview_rating enum_rating
  8. );
英文:

Here I need the default value for my enums if a null value comes.
by default, it is not working. I set the default value while I created the table.
if I pass default in values the default value will appear, but I have both null data and enum data for inputs.

  1. INSERT INTO table(enum_value_1, enum_value_2)
  2. VALUES (?, ?)

currently i am passing like this

  1. INSERT INTO table(enum_value_1, enum_value_2)
  2. VALUES (NULL, 'enum_value')
enum_value_1 enum_value_2
NULL enum_value

but i need data like

enum_value_1 enum_value_2
enum_value_default enum_value

Create table command

  1. CREATE TYPE enum_candidate_status AS ENUM('attended', 'selected', 'rejected', 'not attended');
  2. CREATE TYPE enum_rating AS ENUM('1', '2', '3', '4', '5');
  3. CREATE TABLE CANDIDATE (
  4. candidate_id SERIAL PRIMARY KEY,
  5. first_name varchar(100),
  6. candidate_status enum_candidate_status DEFAULT 'not attended',
  7. interview_rating enum_rating,
  8. );

答案1

得分: 3

为了管理这个,你必须使用函数和触发器。

请注意并按照以下步骤操作。我会使用你的示例来让你理解。

首先创建类型和表:

  1. CREATE TYPE enum_candidate_status AS ENUM('attended', 'selected', 'rejected', 'not attended');
  2. CREATE TYPE enum_rating AS ENUM('1', '2', '3', '4', '5');
  3. CREATE TABLE CANDIDATE (
  4. candidate_id SERIAL PRIMARY KEY,
  5. first_name varchar(100),
  6. candidate_status enum_candidate_status DEFAULT 'not attended',
  7. interview_rating enum_rating
  8. );

然后创建以下函数:

  1. CREATE OR REPLACE FUNCTION force_candidate_status_defaults()
  2. RETURNS trigger
  3. LANGUAGE plpgsql AS
  4. $func$
  5. BEGIN
  6. NEW.candidate_status := 'not attended';
  7. RETURN NEW;
  8. END
  9. $func$;

最后创建触发器来控制你的查询:

  1. CREATE TRIGGER test_table_before_insert
  2. BEFORE INSERT ON CANDIDATE
  3. FOR EACH ROW
  4. WHEN (NEW.candidate_status IS NULL)
  5. EXECUTE FUNCTION force_candidate_status_defaults();

现在你可以测试查询并享受它:

  1. INSERT INTO CANDIDATE(candidate_status, interview_rating) VALUES (NULL, '3') RETURNING *;
英文:

To manage this, you must use the function and trigger

Pay attention and do as follows. I do your example so that it is understandable to you.

at the first time CREATE TYPE and TABLE

  1. CREATE TYPE enum_candidate_status AS ENUM('attended', 'selected', 'rejected', 'not attended');
  2. CREATE TYPE enum_rating AS ENUM('1', '2', '3', '4', '5');
  3. CREATE TABLE CANDIDATE ( candidate_id SERIAL PRIMARY KEY,
  4. first_name varchar(100),
  5. candidate_status enum_candidate_status DEFAULT 'not attended',
  6. interview_rating enum_rating);

then u must be CREATE FUNCTION like below:

  1. CREATE OR REPLACE FUNCTION force_candidate_status_defaults()
  2. RETURNS trigger
  3. LANGUAGE plpgsql AS
  4. $func$
  5. BEGIN
  6. NEW.candidate_status := 'not attended';
  7. RETURN NEW;
  8. END
  9. $func$;

at the end u must CREATE trigger to control on ur queries as follows:

  1. CREATE TRIGGER test_table_before_insert
  2. BEFORE INSERT ON CANDIDATE
  3. FOR EACH ROW
  4. WHEN (NEW.candidate_status IS NULL) -- !
  5. EXECUTE FUNCTION force_candidate_status_defaults();

now u can test query and enjoy it

  1. INSERT INTO CANDIDATE(candidate_status, interview_rating) VALUES (NULL, '3') RETURNING *;

huangapple
  • 本文由 发表于 2022年6月21日 21:41:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/72701874.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定