No binary target

I am following the diesel instructions of how to connect to a database and show posts. I first had to create a lib and Cargo created the application with the following files in the src folder:
lib.rs
schema.rs
models.rs

I then created a file called show_posts.rs where I entered the following code

extern crate diesel_demo;
extern crate diesel;

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

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

    let connection = establish_connection();
    let result = posts.filter(published.eq(true)).limit(5).load::<Post>(&connection).expect("Error loading posts");

    println!("Displaying {} posts", result.len());
    for post in result{
        println!("{}", post.title);
        println!("-------------\n");
        println!("{}", post.body);
    }
}

According to diesel I am able to run the code and I should not get any result cause there are no posts. It says to run the command cargo run --bin show_posts which I did. I got the following error
error: a bin target must be available for cargo run
why am I getting this error pls?
The instructions is on this page Getting Started

Did you put the file in the bin directory?

src/bin/show_posts.rs

Ok thanks that solved it but now I get another error from the schema created by diesel

error[E0277]: the trait bound `i32: FromSql<diesel::sql_types::Unsigned<BigInt>, Mysql>` is not satisfied
  --> src/bin/show_posts.rs:12:60
   |
12 |     let result = posts.filter(published.eq(true)).limit(5).load::<Post>(&connection).expect("Error loading posts");
   |                                                            ^^^^ the trait `FromSql<diesel::sql_types::Unsigned<BigInt>, Mysql>` is not implemented for `i32`

I am using MySQL and not postgres and diesel created that schema file for me

Do you have the mysql feature enabled? The tutorial assumes you're using postgres and only enables that

[dependencies]
diesel = { version = "1.4.4", features = ["mysql"] }
dotenv = "0.15.0"

Here is my toml file

[package]
name = "diesel_demo"
version = "0.1.0"
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
diesel = { version = "1.4.4", features = ["mysql"] }
dotenv = "0.15.0"

You can't use i32 here. Use an u64 instead.

Working thank you could you please tell me why i32 is not working?

Because the database could contain a number that doesn't fit in an i32.

Thank you!

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.