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

OnlyDsl::only() does not work together with aliases when doing inner_join #3616

Open
3 tasks done
Ploppz opened this issue May 1, 2023 · 2 comments
Open
3 tasks done

Comments

@Ploppz
Copy link
Contributor

Ploppz commented May 1, 2023

Setup

Versions

  • Rust: 1.66.1
  • Diesel: 2.0.4
  • Database: Postgres
  • Operating System Arch Linux

Feature Flags

  • diesel: "postgres"

Problem

Compiler error when I try to inner join a table with .only() together with an alias of a different table.

use diesel::dsl::OnlyDsl;
use diesel::prelude::*;
use diesel::sql_types::*;
use diesel::{joinable, table};

table! {
    hci.measurement (id) {
        id -> Int8,
    }
}

table! {
    hci.worker_job (id) {
        id -> Int8,
        measurement_id -> Nullable<Int8>,
    }
}

joinable!(worker_job -> measurement (measurement_id));

allow_tables_to_appear_in_same_query!(measurement, worker_job);

fn main() {
    let (worker_job1, worker_job2) =
        diesel::alias!(worker_job as worker_job1, worker_job as worker_job2);

    // Works well:
    measurement::table.only().inner_join(
        worker_job::table.on(worker_job::measurement_id.eq(measurement::id.nullable())),
    );

    // Does not work:
    measurement::table.only().inner_join(
        worker_job1.on(worker_job1
            .field(worker_job::measurement_id)
            .eq(measurement::id.nullable())),
    );
}

Error:

error[E0277]: the trait bound `query_source::joins::Join<Only<measurement::table>, Alias<worker_job1>, Inner>: AppearsInFromClause<Alias<worker_job1>>` is not satisfied
  --> src/main.rs:33:31
   |
33 |     measurement::table.only().inner_join(
   |                               ^^^^^^^^^^ the trait `AppearsInFromClause<Alias<worker_job1>>` is not implemented for `query_source::joins::Join<Only<measurement::table>, Alias<worker_job1>, Inner>`
   |
   = help: the trait `AppearsInFromClause<T>` is implemented for `query_source::joins::Join<Left, Right, Kind>`
   = note: required for `AliasedField<worker_job1, worker_job::columns::id>` to implement `AppearsOnTable<query_source::joins::Join<Only<measurement::table>, Alias<worker_job1>, Inner>>`
   = note: 2 redundant requirements hidden
   = note: required for `((measurement::columns::id,), (AliasedField<worker_job1, worker_job::columns::id>, AliasedField<worker_job1, worker_job::columns::measurement_id>))` to implement `AppearsOnTable<query_source::joins::Join<Only<measurement::table>, Alias<worker_job1>, Inner>>`
   = note: required for `query_source::joins::Join<Only<measurement::table>, Alias<worker_job1>, Inner>` to implement `QuerySource`
   = note: 1 redundant requirement hidden
   = note: required for `JoinOn<query_source::joins::Join<Only<measurement::table>, Alias<worker_job1>, Inner>, diesel::expression::grouped::Grouped<diesel::expression::operators::Eq<AliasedField<worker_job1, worker_job::columns::measurement_id>, NullableExpression<measurement::columns::id>>>>` to implement `QuerySource`
   = note: required for `SelectStatement<FromClause<Only<measurement::table>>>` to implement `InternalJoinDsl<Alias<worker_job1>, Inner, diesel::expression::grouped::Grouped<diesel::expression::operators::Eq<AliasedField<worker_job1, worker_job::columns::measurement_id>, NullableExpression<measurement::columns::id>>>>`

Checklist

  • This issue can be reproduced on Rust's stable channel. (Your issue will be
    closed if this is not the case)
  • This issue can be reproduced without requiring a third party crate
@Ploppz Ploppz added the bug label May 1, 2023
@JainVidit12
Copy link

I came across this issue while I am trying to learn rust. Can I take this up?

@weiznich
Copy link
Member

@JainVidit12 We are generally happy to get contributions for any open issue, so yes you can take up this one. That written: This issue likely requires debugging some complicated trait setup. I'm not sure whether that's a good issue for someone still learning rust. That shouldn't stop you from working on this issue, I just want to avoid potential frustration.
Now to write something about the actual issue: I would start with checking for similar trait impls that that ones listed in the required for section of the error message. Likely one of the impls there is missing or to restrictive.

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

No branches or pull requests

3 participants