Skip to content

More elegant way to compare enum in postgres? #235

Answered by billy1624
phungleson asked this question in Q&A
Discussion options

You must be logged in to vote

Hey @phungleson, you can do it with as_enum method

let query = Query::select()
    .expr(Expr::col(Char::FontSize).as_enum(Alias::new("text")))
    .from(Char::Table)
    .to_owned();

assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"SELECT CAST("font_size" AS text) FROM "character""#
);
let query = Query::insert()
    .into_table(Char::Table)
    .columns(vec![Char::FontSize])
    .exprs_panic(vec![Expr::val("large").as_enum(Alias::new("FontSizeEnum"))])
    .to_owned();

assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"INSERT INTO "character" ("font_size") VALUES (CAST('large' AS …

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by billy1624
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #234 on January 14, 2022 06:24.