diff --git a/diesel/src/query_source/aliasing/aliased_field.rs b/diesel/src/query_source/aliasing/aliased_field.rs index 1e5db2f8f625..82c2ecb8b00e 100644 --- a/diesel/src/query_source/aliasing/aliased_field.rs +++ b/diesel/src/query_source/aliasing/aliased_field.rs @@ -1,3 +1,5 @@ +use diesel_derives::DieselNumericOps; + use super::{Alias, AliasSource}; use crate::backend::Backend; @@ -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. diff --git a/diesel_tests/tests/alias.rs b/diesel_tests/tests/alias.rs index 2f933e588cce..265fecbe7db2 100644 --- a/diesel_tests/tests/alias.rs +++ b/diesel_tests/tests/alias.rs @@ -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();