小编And*_*rey的帖子

postgresql:枚举和字符变化,更新

我在postgresql中有ENUM类型

CREATE TYPE user_state AS ENUM ('unconfirmed', 'locked', 'active');
Run Code Online (Sandbox Code Playgroud)

我有一个字段stateusers与类型user_state.

我尝试执行下一个查询:

UPDATE "users" SET "state" = 'active'::character varying WHERE "id" = 1 (*1)
Run Code Online (Sandbox Code Playgroud)

但是查询失败了.错误是:Column "state" is of type user_state but expression is of type character varying. Hint: You will need to rewrite or cast the expression.

好吧,有一些阅读勒芒,写了一些代码,我有一个隐式的类型转换为字符变化到user_state:

CREATE OR REPLACE FUNCTION
    character_varying_to_user_status(user_state)
    RETURNS user_state AS $$
    SELECT $1::user_state
    $$ LANGUAGE SQL;

CREATE CAST (character varying AS user_state)
    WITH FUNCTION character_varying_to_user_status (character varying)
    AS …
Run Code Online (Sandbox Code Playgroud)

postgresql enums

3
推荐指数
1
解决办法
4243
查看次数

标签 统计

enums ×1

postgresql ×1