Skip to content

Commit

Permalink
Merge pull request #3979 from formlogic-kirk/alias_op_impls
Browse files Browse the repository at this point in the history
Alias op impls
  • Loading branch information
weiznich authored Apr 5, 2024
2 parents ac43e5b + ee91d91 commit 9fe91c3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
4 changes: 3 additions & 1 deletion diesel/src/query_source/aliasing/aliased_field.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use diesel_derives::DieselNumericOps;

use super::{Alias, AliasSource};

use crate::backend::Backend;
Expand All @@ -13,7 +15,7 @@ use crate::sql_types;

use std::marker::PhantomData;

#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone, Copy, DieselNumericOps)]
/// Represents an aliased field (column) within diesel's query builder
///
/// See [`alias!`](crate::alias) for more details.
Expand Down
19 changes: 19 additions & 0 deletions diesel_tests/tests/alias.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,25 @@ fn selecting_basic_data() {
assert_eq!(expected_data, actual_data);
}

#[test]
fn ops_with_aliases() {
// This test should fail to compile if the std::ops::{Add, Sub, ...} impls are missing for AliasedField.
let likes_alias = alias!(likes as likes_alias);
let pokes_alias = alias!(pokes as pokes_alias);

// Using pokes::poke_count and comment_id as they are columns of the same type
let _unaliased = likes::table
.inner_join(pokes::table.on(likes::user_id.eq(pokes::user_id)))
.select(pokes::poke_count + likes::comment_id);
let _aliased = likes_alias
.inner_join(
pokes_alias.on(likes_alias
.field(likes::user_id)
.eq(pokes_alias.field(pokes::user_id))),
)
.select(pokes_alias.field(pokes::poke_count) + likes_alias.field(likes::comment_id));
}

#[test]
fn select_multiple_from_join() {
let connection = &mut connection_with_sean_and_tess_in_users_table();
Expand Down

0 comments on commit 9fe91c3

Please sign in to comment.