Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why "no method named `execute` found" when using diesel::insert_into?

I have a problem when I use diesel::insert_into to insert data into the database:

table! {
    post (id) {
        id -> Integer,
        username -> Varchar,
        postdata -> Varchar,
    }
}
pub fn create_new_post(post: Post, conn: DbConn) {
    diesel::insert_into(post::table)
            .values(&post)
            .execute(&*conn);
}
6 |             .execute(&*conn);
  |              ^^^^^^^ method not found in `InsertStatement<table, ValuesClause<(ColumnInsertValue<columns::id, diesel::expression::bound::Bound<diesel::sql_types::Integer, &i32>>, ColumnInsertValue<columns::username, diesel::expression::bound::Bound<diesel::sql_types::Text, &std::string::String>>, ColumnInsertValue<columns::postdata, diesel::expression::bound::Bound<diesel::sql_types::Text, &std::string::String>>), table>>`
  |
  = help: items from traits can only be used if the trait is in scope
  = note: the following trait is implemented but not in scope; perhaps add a `use` for it:
          `use crate::diesel::RunQueryDsl; 

Can anyone help me?

like image 327
awbt Avatar asked Oct 13 '25 00:10

awbt


1 Answers

As the compiler error message is telling you, you miss a use crate::diesel::RunQueryDsl; in your current module. That means the corresponding trait is not in scope.

like image 76
weiznich Avatar answered Oct 14 '25 18:10

weiznich