-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#[sqlx(json)] does not support nullable values #2849
Comments
What is the error? |
UnexpectedNullError: |
Okay, that's why I asked because on first blush I thought you were getting a compiler error. That's why it's important to specify exactly what error you're getting. So the problem is that Now, it's quite possible that someone might actually want it to work that way, so unfortunately we can't just change it to output However, there might be another option. I think it would be relatively easy to specialize I'm not sure if that would be too surprising or not. Do you think anyone would actually ever need to distinguish between |
I'm sorry for the confusion, I won't forget it next time :D
I cannot think of any situation where someone would need to distinguish between the these, so I really like this solution. Another possbile solution would be something like |
Yeah, |
Thinking about this more, I'm wary of quietly changing the behavior of |
@abonander is there any update on this? I am getting the same error. |
We'd always welcome a pull request. |
I was also impacted by this today! I don't know what to do now. Why is an This is really strange. Anyway thanks all of you for your hard work! ♥ |
for now I done this in inelegant way and hope there will be official way for this
CASE WHEN songs.singer_id IS NOT NULL THEN
json_build_object('id', a3.id, 'name', a3.name)
ELSE
NULL
END AS singer #[derive(Debug, PartialEq, Deserialize, Serialize, sqlx::FromRow)]
pub struct Artist {
pub id: Uuid,
pub name: String,
pub created_at: DateTime<Utc>,
}
#[derive(Debug, PartialEq, Deserialize, Serialize)]
pub struct Song {
pub id: Uuid,
pub name: String,
pub singer: Option<Artist>,
pub updated_at: DateTime<Utc>,
}
impl FromRow<'_, PgRow> for Song {
fn from_row(row: &PgRow) -> sqlx::Result<Self> {
Ok(Self {
id: row.get("id"),
name: row.get("name"),
// here we check if null
singer: match row.try_get::<JsonValue, &str>("singer") {
Ok(singer) => Some(Artist::deserialize::<JsonValue>(singer).unwrap()),
Err(_) => None,
},
updated_at: row.get("updated_at"),
})
}
} |
Bug Description
When trying to use
#[sqlx(json)]
on a nullable column, an error is thrownMinimal Reproduction
Info
rustc --version
: 1.73.0The text was updated successfully, but these errors were encountered: