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?