0
votes

I want to insert a record into PostgreSQL 13 database using rust diesel diesel = { version = "1.4.4", features = ["postgres"] } , this is the code I am write now:

let new_song = Songs {
        id: 0,
        name: (record.title).to_string(),
        artists: artist_json.to_string(),
        album_id: record.album.id.to_string().parse().unwrap(),
        publishtime: 0,
        status: 1,
        duration: 0,
        source_id: record.id.to_string(),
        source: 1,
        created_time: timestamp,
        updated_time: timestamp
    };
    // save songs
    diesel::insert_into(songs::table)
        .values(&new_song)
        .on_conflict_do_nothing()
        .execute(&connection)
        .unwrap();

this could compile success, but when running shows error like this:

thread 'rocket-worker-thread' panicked at 'called `Result::unwrap()` on an `Err` value: DatabaseError(__Unknown, "cannot insert into column \"id\"")', src/biz/music/music.rs:133:10

to my understand the id was auto genertate. I should not write a value into id. But when I remove the id when insert into database. the code could not compile success, shows error like this:

error[E0277]: the trait bound `(std::string::String, std::string::String, i64, i64, i32, i32, std::string::String, i32, i64, i64): Queryable<(BigInt, diesel::sql_types::Text, diesel::sql_types::Text, BigInt, BigInt, Integer, Integer, diesel::sql_types::Text, Integer, BigInt, BigInt), _>` is not satisfied
  --> src/biz/music/songs.rs:23:10
   |
23 |         .load::<Songs>(&connection)
   |          ^^^^ the trait `Queryable<(BigInt, diesel::sql_types::Text, diesel::sql_types::Text, BigInt, BigInt, Integer, Integer, diesel::sql_types::Text, Integer, BigInt, BigInt), _>` is not implemented for `(std::string::String, std::string::String, i64, i64, i32, i32, std::string::String, i32, i64, i64)`
   |
   = help: the following implementations were found:
             <(A, B, C, D, E, F, G, H, I, J) as Queryable<(SA, SB, SC, SD, SE, SF, SG, SH, SI, SJ), __DB>>
             <(A, B, C, D, E, F, G, H, I, J) as Queryable<diesel::sql_types::Record<(SA, SB, SC, SD, SE, SF, SG, SH, SI, SJ)>, Pg>>
note: required because of the requirements on the impl of `Queryable<(BigInt, diesel::sql_types::Text, diesel::sql_types::Text, BigInt, BigInt, Integer, Integer, diesel::sql_types::Text, Integer, BigInt, BigInt), _>` for `songs::models::Songs`
  --> src/biz/music/../../models.rs:22:31
   |
22 | #[derive(Insertable,Serialize,Queryable)]
   |                               ^^^^^^^^^
23 | #[table_name="songs"]
24 | pub struct Songs {
   |            ^^^^^
   = note: required because of the requirements on the impl of `LoadQuery<_, songs::models::Songs>` for `diesel::query_builder::SelectStatement<songs::schema::songs::table, query_builder::select_clause::DefaultSelectClause, query_builder::distinct_clause::NoDistinctClause, query_builder::where_clause::WhereClause<diesel::expression::operators::Eq<songs::schema::songs::columns::source_id, diesel::expression::bound::Bound<diesel::sql_types::Text, &str>>>, query_builder::order_clause::NoOrderClause, query_builder::limit_clause::LimitClause<diesel::expression::bound::Bound<BigInt, i64>>>`
   = note: this error originates in the derive macro `Queryable` (in Nightly builds, run with -Z macro-backtrace for more info)

For more information about this error, try `rustc --explain E0277`.
warning: `reddwarf_music` (bin "reddwarf_music") generated 1 warning
error: could not compile `reddwarf_music` due to previous error; 1 warning emitted

how make diesel ignore the id or remove the id fields when insert data into PostgreSQL database? what should I do?

Hello, you need to create a specific struct implementing Insertable, for more see diesel.rs/guides/getting-startedZeppi
I alredy do it like that, but I did not figure out why design like this. why define the specific insert model? @Zeppi Why would not use the same model?Dolphin
I don't know exactly why. I think the answer lies in studying the Insertable Trait. It is common that in ORMs the ID field is set by default as the key. it starts to get twisted when you pick another key name first or outright if it's tables where the unique key is a key pair composed.Zeppi