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

[feature request] Support nested Queryable, Identifiable (and like those) #1006

Closed
KeenS opened this Issue Jul 7, 2017 · 0 comments

Comments

Projects
None yet
2 participants
@KeenS
Contributor

KeenS commented Jul 7, 2017

Current diesel doesn't support nested Queryables nor Identifiables but I'd like to write code like below:

#[derive(Eq, PartialEq, Hash)]
#[derive(Queryable)]
struct UserId(pub i32);

#[derive(Queryable, Identifiable)]
struct User {
    // nested Queryable
    pub id: UserId,
    pub name: String,
}

#[derive(Queryable, Identifiable)]
#[table_name = "users"]
struct UserDetail {
    // nested Queryable, Identifiable.
    //  May need a certain attribute like `#[have_primary_key(id)]`
    pub user: User,
    pub email_address: String,
    pub screen_name: String,
}

fn find_user(cn: &SomeConnection, user_id: UserId) -> Result<User, Error> {
    //...
    // If `UserId` is `AsExpression` and can be use like `find(user_id)`, it's also awesome but not scope of this issue
    users.find(user_id.0)
        .select(((id), name))
        // or, allowing `select((id, name))` will be useful
        .get_result(cn)
 
}
fn find_user_detail(cn: &SomeConnection, user_id: UserId) -> Result<UserDetail, Error> {
    //...
    users.find(user_id.0)
        .select((((id), name), email_address, screen_name))
        // the same above. allowing `select((id, name, email_address, screen_name))` will be usefull
        .get_result(cn)
}

In summary,

  • A struct is Queryable when its direct fields and fields of its child structs are isomorphic to the selecting sql fields
  • A struct is Identifiable when it has a primary key or one of its child structs have a primary key

As far as Queryable is concerned, nested deriving itself is possible but impossible to get_result from any isomorphic sql types. (This may not the matter of Queryabel but the matter of FromSql and ToSql.)

Structs like struct UserId(i32); is very useful to avoid confusions when handling many table ids. Is it possibel to implement these features?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment