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

Is `HasMany!` still required for joins? #833

Closed
jnferner opened this Issue Mar 31, 2017 · 2 comments

Comments

Projects
None yet
2 participants
@jnferner

jnferner commented Mar 31, 2017

The macro HasMany!

  • isn't documented much (Do you have to treat the participating structs different afterwards?)
  • is not tested anywhere in the repo
  • has special annotation syntax (#[table_name(name)] vs #[table_name="name"])
  • Hasn't been significantly touched in a while
  • requires you to duplicate your code if you need the parent struct to be pub, e.g.
#[table_name="question"]
#[derive(Queryable, Identifiable)]
pub struct Question {
    pub id: i32,
    pub category_id: i32,
    pub text: String,
}

#[table_name="category"]
#[derive(Queryable, Identifiable, Debug)]
pub struct Category {
    pub id: i32,
    pub text: String,
}

HasMany! {
    (question, foreign_key = category_id)
    #[table_name(category)]
    #[derive(Queryable, Identifiable, Debug)]
    pub struct Category {
        pub id: i32,
        pub text: String,
    }
}

The last point makes me very uncomfortable with the macro, As I have many 1:n relations in my database.
Is there an alternative?
Did I understand something wrong?

@sgrif

This comment has been minimized.

Member

sgrif commented Mar 31, 2017

isn't documented much

It's documented at http://docs.diesel.rs/diesel/associations/index.html

(Do you have to treat the participating structs different afterwards?)

No

is not tested anywhere in the repo

yes it is

has special annotation syntax

This is the same as all other derives

Hasn't been significantly touched in a while

Unsure why this is something that you have a problem with?

Is there an alternative?

Not using it. All #[has_many] does right now is enable parent_table.inner_join(child_table). If you always join in the reverse direction, or use belonging_to and grouped_by, you don't need it.

@sgrif sgrif closed this Mar 31, 2017

@jnferner

This comment has been minimized.

jnferner commented Mar 31, 2017

That was quick! Thank you for your help. Really enjoying diesel so far :)

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