From c5bc8e1ce44d4f986e1fc77df0bbe055a72723b8 Mon Sep 17 00:00:00 2001 From: kirk Date: Fri, 5 Apr 2024 12:37:05 -0500 Subject: [PATCH 1/2] add failing test for addition of AliasedField --- diesel_tests/tests/alias.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) 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(); From ee91d91c4fdcfcc72499bdc516aeb11e35eba2a0 Mon Sep 17 00:00:00 2001 From: kirk Date: Fri, 5 Apr 2024 12:39:47 -0500 Subject: [PATCH 2/2] add derive of DieselNumericOps to AliasedField --- diesel/src/query_source/aliasing/aliased_field.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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.