I'm trying to create a new Postgres jsonb value and insert into the database:
let datadoc = Some(json!("a string"));
new_photo(&connection, datadoc);
And I'm getting this error:
15 | let datadoc = Some(json!("a string"));
| ^^^^^^^ expected enum std::option::Option, found struct database::schema::photo::datadoc
|
= note: expected type std::option::Option<serde_json::Value>
found type database::schema::photo::datadoc
The issue seems like diesel generates a struct called datadoc, and you have a local binding with the same name. Try changing your datadoc local to some other name.
When you invoke table!, we create a bunch of data structures to represent your schema. (See the guide Schema in Depth if you're interested in the specifics!). In particular, we create a struct to represent each column in your table. We also define a dsl module for convenience, which I'm assuming you glob imported based on your error message. That module looks like this:
pub mod dsl {
pub use super::table as photo;
pub use super::columns::*;
}
The second line is the important one. Inside the columns module, is a struct that looks like this:
pub struct datadoc;
This means there is a struct in scope with the name datadoc at the place where you're receiving an error message. One thing that might not be obvious if you're new to rust is that all let bindings are actually pattern matches. That's why you can write code like let (x, y) = some_tuple;. With that in mind, and knowing that datadoc is a struct with 0 fields, we can see that your code is being interpreted by rustc as:
let datadoc {} = Some(json!("a string"))
There are two solutions to this. The first is to simply change the name of your variable as others have mentioned. The other is to replace the line use schema::photos::dsl::*; with use schema::photos;, and reference photos as photos::table, and any columns as photos::column.