最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

rust - Rename enum value for use with sqlx in query - Stack Overflow

programmeradmin1浏览0评论

I'm using Postgres. I want to store char in the a column that will give the "type" of the row. But I also want the code to be more expressive, so I don't want to create enums with char values, but actual strings.

This is the table:

CREATE TABLE
    chats (
        id UUID PRIMARY KEY DEFAULT uuid_generate_v4 (),
        name VARCHAR(100) NOT NULL,
        -- p: one-on-one, g: group, b: broadcast, s: service
        type CHAR(1) NOT NULL,
        created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
        updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW()
    );

This is how I'd like to use it:

#[derive(sqlx::Type, Debug, Serialize, Deserialize)]
pub enum ChatType {
    #[sqlx(rename="p")]
    Personal, 
    #[sqlx(rename="g")]
    Group,
    #[sqlx(rename="b")]
    Broadcast,
    #[sqlx(rename="s")]
    Service,
}

There is a #[sqlx(rename="name")], but that didn't do what I expected. I get the following error at runtime (unfortunately, it compiles fine):

Error: error returned from database: type "chattype" does not exist

Caused by:
    type "chattype" does not exist

Stack backtrace:
   0: anyhow::error::<impl core::convert::From<E> for anyhow::Error>::from

Can it be achieved?

I'm using Postgres. I want to store char in the a column that will give the "type" of the row. But I also want the code to be more expressive, so I don't want to create enums with char values, but actual strings.

This is the table:

CREATE TABLE
    chats (
        id UUID PRIMARY KEY DEFAULT uuid_generate_v4 (),
        name VARCHAR(100) NOT NULL,
        -- p: one-on-one, g: group, b: broadcast, s: service
        type CHAR(1) NOT NULL,
        created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
        updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW()
    );

This is how I'd like to use it:

#[derive(sqlx::Type, Debug, Serialize, Deserialize)]
pub enum ChatType {
    #[sqlx(rename="p")]
    Personal, 
    #[sqlx(rename="g")]
    Group,
    #[sqlx(rename="b")]
    Broadcast,
    #[sqlx(rename="s")]
    Service,
}

There is a #[sqlx(rename="name")], but that didn't do what I expected. I get the following error at runtime (unfortunately, it compiles fine):

Error: error returned from database: type "chattype" does not exist

Caused by:
    type "chattype" does not exist

Stack backtrace:
   0: anyhow::error::<impl core::convert::From<E> for anyhow::Error>::from

Can it be achieved?

Share Improve this question edited Feb 23 at 4:22 Yuri Astrakhan 10k8 gold badges72 silver badges117 bronze badges asked Feb 10 at 17:21 jpennajpenna 9,2115 gold badges30 silver badges40 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 1

Found out it can! And it was very simple...

The rename in the enum value works, but I had to add one line:

#[derive(sqlx::Type, Debug, Serialize, Deserialize)]
#[sqlx(type_name="CHAR")]
pub enum ChatType {
    #[sqlx(rename = "p")]
    Personal,
    #[sqlx(rename = "g")]
    Group,
    #[sqlx(rename = "b")]
    Broadcast,
    #[sqlx(rename = "s")]
    Service,
}

#[sqlx(type_name="CHAR")] says that the type for the enum is CHAR, which overrides the default type for the enum with string values, which is VARCHAR.

By the way, if you use sqlx::Type but don't define the sqlx(type_name...) trait, it will default to expecting a custom enum defined in the database with the name of the enum, in this case "chattype" (you will get the error "type "chattype" does not exist").

发布评论

评论列表(0)

  1. 暂无评论