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

Use joinable! macro to join using non PK columns #938

Closed
PritiKumr opened this Issue Jun 7, 2017 · 3 comments

Comments

Projects
None yet
3 participants
@PritiKumr

PritiKumr commented Jun 7, 2017

Trying to use the joinable! macro for this issue on crates.io - rust-lang/crates.io#740

We're trying to join the following tables -

crate_downloads
(
  crate_id integer NOT NULL,
  downloads integer NOT NULL,
  date date NOT NULL,
  CONSTRAINT crate_downloads_pkey PRIMARY KEY (crate_id, date),
)
CREATE TABLE public.crate_owners
(
  crate_id integer NOT NULL,
  owner_id integer NOT NULL,
  owner_kind integer NOT NULL,
  CONSTRAINT crate_owners_pkey PRIMARY KEY (crate_id, owner_id, owner_kind),
)

Note - removed few columns for clarity.

I want to join these two tables on just the crate_id column. But this is part of a composite key on crate_downloads. So when we do -

joinable!(crate_owners -> crate_downloads (crate_id));

I get the error -

error[E0271]: type mismatch resolving `<diesel::expression::nullable::Nullable<(schema::crate_downloads::columns::crate_id, schema::crate_downloads::columns::date)> as diesel::Expression>::SqlType == diesel::types::Nullable<diesel::types::Integer>`
   --> src/user/mod.rs:391:5
    |
391 |     joinable!(crate_owners -> crate_downloads (crate_id));
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected tuple, found struct `diesel::types::Integer`
    |
    = note: expected type `diesel::types::Nullable<(diesel::types::Integer, diesel::types::Date)>`
               found type `diesel::types::Nullable<diesel::types::Integer>`
    = note: this error originates in a macro outside of the current crate

Appreciate some guidance here. :)

@sgrif

This comment has been minimized.

Member

sgrif commented Jun 8, 2017

In theory we could allow this, but it would be more true to the associations API for this to go through the crates table, since that is the primary key both foreign keys are referencing. So this is at least partially a duplicate of the issues for adding through associations, but I'm going to leave this open since this is specific to declaring possible joins.

@steverob

This comment has been minimized.

steverob commented Jun 8, 2017

@sgrif it makes sense to go for a through association for this. But, would it not be nice to let the developers choose the appropriate approach here and diesel giving them all options? :)

@sgrif

This comment has been minimized.

Member

sgrif commented Dec 16, 2017

This was fixed by 5cd4c0c

@sgrif sgrif closed this Dec 16, 2017

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