How can i write Select query in diesel?

How can I convert to diesel:
SELECT username FROM table WHERE username = 1$

I tried this code :
let user = diesel::dsl::select(user_data::table.select(user_data::username))
.filter(user_data::username.eq(username))
.get_result(&mut conn)
.ok();

But got an error: .select(user_data::username))
^^^^^^ method cannot be called due to unsatisfied trait bounds

I've never seen such a redundant double-select before; you must be using Diesel the wrong way. If you look at the Getting Started guide, this is what a basic select looks like:

use self::models::*;
use diesel::prelude::*;
use diesel_demo::*;

fn main() {
    use self::schema::posts::dsl::*;

    let connection = &mut establish_connection();
    let results = posts
        .filter(published.eq(true))
        .limit(5)
        .select(Post::as_select())
        .load(connection)
        .expect("Error loading posts");
}
1 Like

By the way, you can post code between two ``` lines to make it a code block

```
let user = user_data::table.select(user_data::username)
.filter(user_data::username.eq(username))
.get_result(&mut conn)
.ok();
```
turns into

let user = user_data::table.select(user_data::username)
    .filter(user_data::username.eq(username))
    .get_result(&mut conn)
    .ok();

This is much better than posting code as images. It would also be useful to see more context, such as what user_data is. It looks like you may have imported user_data::dsl::* but are still trying to use the non-dsl way of writing the query, additionally with the mistake of adding in an unnecessary diesel::dsl::select. Removing that and just importing schema::user_data might fix your problem.

2 Likes

Thank You so much, Yes you are right i am using diesel in wrong way, and thanks for helping

thank you so much

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.