Array with custom type fails to insert #4157
Replies: 4 comments 4 replies
-
Can you provide the actual error message contained in the result? You should be able to do that by adding a |
Beta Was this translation helpful? Give feedback.
-
Here you go. I am perplexed to see a wrong number of columns error. Any ideas or suggestions how that could happen? |
Beta Was this translation helpful? Give feedback.
-
Thank you @weiznich Following the documentation, I have re-written the ToSql, see below, and made it work. However, the Enum in the composite type didn't serialize at all so ultimately, I changed it to an Integer and then things worked out. Thank you so much for your speedy help. impl ToSql<PgServiceEndpoint, Pg> for Endpoint {
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
WriteTuple::<(Text, Integer, Text, Integer, Integer)>::write_tuple(
&(
self.name.to_owned(),
self.version.to_owned(),
self.base_uri.to_owned(),
self.port.to_owned(),
self.protocol.to_owned(),
),
&mut out.reborrow(),
)
}
}
impl FromSql<PgServiceEndpoint, Pg> for Endpoint {
fn from_sql(bytes: PgValue<'_>) -> deserialize::Result<Self> {
let (name, version, base_uri, port, protocol) = FromSql::<
Record<(Text, Integer, Text, Integer, Integer)>,
Pg,
>::from_sql(bytes)?;
Ok(Endpoint {
name,
version,
base_uri,
port,
protocol,
})
}
} |
Beta Was this translation helpful? Give feedback.
-
@weiznich Ok, I understand that the documentation has a gap w.r.t. to bringing everything related to custom types together. In response, I prepared a PR that contributes back example code with a comprehensive Readme that covers the bulk of the concepts and how they relate to each other. Would you please review the draft PR: #4169 |
Beta Was this translation helpful? Give feedback.
-
rust-version = "1.80.0"
diesel = { version = "2.2.2", features = ["postgres", "r2d2"] }
In the code below, I can insert an empty array in my table,
but when I try to insert an array with an actual custom type, the insert fails.
What I have tried to solve the issue:
When I insert an empty Array, I see all the data in my table with the array being empty,
therefore I suspect something with the SQL serialization of the custom type might be wrong.
I did searched the issue tracker and forum, but I have't found a solution. The closest I found is
a blogpost from 2020 written for Diesel 1.4, but Diesel has changed a lot in the meantime.
However, because all the code below compiles, and the error from the insert is rather opaque,
I am unsure what the cause of the problem might be.
SQL:
Schema:
Custom Enum Type:
Custom Type used in the array:
Service mapped to table and the insert implementation:
And the failing insert:
The error message is rather short on details:
Any idea how to fix the insert?
Any help or links would be much appreciated.
Beta Was this translation helpful? Give feedback.
All reactions