Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to implemt FromSql trait for type like Role #1124

Open
HosMercury opened this issue Mar 26, 2024 · 2 comments
Open

How to implemt FromSql trait for type like Role #1124

HosMercury opened this issue Mar 26, 2024 · 2 comments

Comments

@HosMercury
Copy link

How to implemt FromSql trait for type like Role

pub struct Role {
    pub uid: Uuid,
    pub name: String,
}
@sfackler
Copy link
Owner

What value do you want that to turn into in the database?

@HosMercury
Copy link
Author

HosMercury commented Mar 26, 2024

Thx for fast reply

    pub async fn with_roles(&self, state: &AppState) -> Result<()> {
        let pool = state.pool.clone();
        let sql = r#"
            SELECT users.* , Arr_AGG(roles.*) AS roles
            FROM users
            JOIN users_roles ON users_roles.user_uid = users.uid
            JOIN roles ON roles.uid = users_roles.role_uid
            WHERE users.uid = $1
            GROUP BY users.uid
            "#;

        let conn = pool.get().await.unwrap();
        // use the connection
        // it will be returned to the pool when it falls out of scope.

        let rows = conn.query(sql, &[&self.uid]).await.unwrap();

        // let value: &str = rows[0].get(0);


        Ok(())
    }

I'm getting array from postgres
I get Vec it as array
I want to convert it to Vec<Role> ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants