Skip to content
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

Indexing serde_json::Value during query-building #1823

Closed
Kibouo opened this issue Aug 13, 2018 · 2 comments
Closed

Indexing serde_json::Value during query-building #1823

Kibouo opened this issue Aug 13, 2018 · 2 comments

Comments

@Kibouo
Copy link

Kibouo commented Aug 13, 2018

I'm trying to remove expired sessions from a postgresql db.

The scheme looks like this:

table! {
    session (key) {
        key -> Uuid,
        data -> Jsonb,
    }
}

And the table as follows:

#[derive(Queryable, Insertable, AsChangeset)]
#[table_name = "session"]
pub struct Session
{
    pub key:  ::uuid::Uuid,
    pub data: ::serde_json::Value
}

The data field is always filled in using following (to-be extended) struct:

use chrono::{DateTime, Utc};
pub struct SessionData
{
    pub deadline: DateTime<Utc>
}

I wanted to index the data.deadline aka SessionData.deadline field, and delete expired sessions as such:

diesel::delete(session.filter(data["deadline"].st(now))).execute(&*db_conn)

This doesn't work. It returns the compile-error

error[E0608]: cannot index into a value of type `db::schema::session::session::columns::data`
  --> web/src/main.rs:65:55
   |
65 |                         diesel::delete(session.filter(data["deadline"].st(now)))
   |                                                       ^^^^^^^^^^^^^^^^

How can this be done? I can't seem to find any documentation that uses indexing in a query.

Versions

  • Rust: rustc 1.30.0-nightly (73c78734b 2018-08-05)
  • Diesel: "1.3.2"
  • serde: "1.0.70"
  • serde_json: "1.0.24"
  • Database: postgresql 10.5
  • Operating System: Arch Linux x86_64 4.17.12-arch1-1-ARCH

Feature Flags

  • diesel: "postgres", "r2d2", "serde_json", "uuid"
@sgrif
Copy link
Member

sgrif commented Aug 14, 2018

#44 tracks json operator support

@sgrif sgrif closed this as completed Aug 14, 2018
@Kibouo
Copy link
Author

Kibouo commented Aug 14, 2018

At the time of asking this question, json operators are not implemented yet. A work-around is using sql() to write raw SQL code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants