You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've create a little demo app, trying out sqlx with xid for primary keys. Since it contains the creation timestamp, this eliminates the need for created_at columns, which seems pretty neat - like ULIDs or some versions of UUIDs.
When I write a query like this:
WITH created_order AS (
INSERT INTO orders (id)
VALUES (xid())
RETURNING id, xid_time(id) as created_at
)
SELECT id, created_at FROM created_order
in an axum handler:
#[derive(sqlx::FromRow,Serialize)]pubstructOrderWithCreatedAt{pubid:String,pubcreated_at:DateTime<Utc>,}asyncfncreate_order(DatabaseConnection(mut connection):DatabaseConnection,Json(body):Json<CreateOrder>) -> Result<Json<OrderWithCreatedAt>,(StatusCode,String)>{let order = sqlx::query_as!(OrderWithCreatedAt,// language=PostgreSQLr#" WITH created_order AS ( INSERT INTO orders (id) VALUES (xid()) RETURNING id, xid_time(id) as created_at ) SELECT id, created_at FROM created_order "#,).fetch_one(&mut*connection).await.map_err(internal_error)?;Ok(Json(order))}
I get this compiler error:
error[E0277]: the trait bound `std::string::String: From<std::option::Option<DateTime<Utc>>>` is not satisfied
--> src/main.rs:82:17
|
82 | let order = sqlx::query_as!(
| _________________^
83 | | OrderWithCreatedAt,
84 | | // language=PostgreSQL
85 | | r#"
... |
93 | |
94 | | )
| |_____^ the trait `From<std::option::Option<DateTime<Utc>>>` is not implemented for `DateTime<Utc>`
I think this makes sense, but I would like to coerce the type to non-optional. Is that possible?
(The compiler error persists, e.g., if I modify the query to COALESCE(xid_time(id), NOW()))
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I've create a little demo app, trying out sqlx with xid for primary keys. Since it contains the creation timestamp, this eliminates the need for
created_at
columns, which seems pretty neat - like ULIDs or some versions of UUIDs.When I write a query like this:
in an axum handler:
I get this compiler error:
I think this makes sense, but I would like to coerce the type to non-optional. Is that possible?
(The compiler error persists, e.g., if I modify the query to
COALESCE(xid_time(id), NOW())
)Beta Was this translation helpful? Give feedback.
All reactions