-
Hi, I have this schema: CREATE TYPE profile AS ENUM ('web', 'user-agent-based', 'native');
CREATE TABLE clients (
id SERIAL PRIMARY KEY,
client_id VARCHAR NOT NULL UNIQUE,
client_profile profile NOT NULL
); pub mod sql_types {
#[derive(diesel::query_builder::QueryId, Clone, diesel::sql_types::SqlType)]
#[diesel(postgres_type(name = "profile"))]
pub struct Profile;
}
diesel::table! {
use diesel::sql_types::*;
use super::sql_types::Profile;
clients (id) {
id -> Int4,
client_id -> Varchar,
client_profile -> Profile,
}
} I am trying to find a way to work with this enum type in the model and I don't understand how to write #[derive(Debug, Clone, Copy, AsExpression)]
#[diesel(sql_type = sql_types::Profile)]
pub enum Profile {
Web,
UserAgentBased,
Native,
}
impl<DB> ToSql<sql_types::Profile, DB> for Profile
where
DB: Backend,
{
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, DB>) -> serialize::Result {
match self {
Profile::Web => "web".to_owned().to_sql(out),
Profile::UserAgentBased => "user-agent-based".to_owned().to_sql(out),
Profile::Native => "native".to_owned().to_sql(out),
}
}
}
#[derive(Queryable, Selectable)]
#[diesel(table_name = clients)]
#[diesel(check_for_backend(diesel::pg::Pg))]
pub struct Client {
pub id: i32,
pub client_id: String,
pub client_profile: Profile,
} The build fails with these errors:
Is it possible to use string instead integer ? Could you give me an example ? |
Beta Was this translation helpful? Give feedback.
Answered by
YievCkim
Nov 29, 2024
Replies: 1 comment 2 replies
-
There is a complete example here in the repo. |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It works thank you !