More elegant way to compare enum in postgres? #235
Answered
by
billy1624
phungleson
asked this question in
Q&A
-
Basically I have enum This throw error
This doesn't work as .and_where(
Expr::col(X::Type).eq(Func::cast_as(type_.clone(), Alias::new(TYPE)))
) Only this work. .and_where(
Func::cast_as(type_.clone(), Alias::new(TYPE))
.equals(Expr::col(X::Type)),
) I wonder if we should make second case |
Beta Was this translation helpful? Give feedback.
Answered by
billy1624
Jan 14, 2022
Replies: 1 comment
-
Hey @phungleson, you can do it with 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 FontSizeEnum))"#
); |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
billy1624
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey @phungleson, you can do it with
as_enum
method