@@ -65,12 +65,12 @@ macro_rules! impl_Identifiable {
6565 body = $( $body: tt) *
6666 ) ,
6767 found_option_with_name = primary_key,
68- value = ( $primary_key_name : ident ) ,
68+ value = $primary_key_names : tt ,
6969 ) => {
7070 impl_Identifiable! {
7171 (
7272 table_name = $table_name,
73- primary_key_name = $primary_key_name ,
73+ primary_key_names = $primary_key_names ,
7474 )
7575 $( $body) *
7676 }
@@ -83,13 +83,14 @@ macro_rules! impl_Identifiable {
8383 struct_ty = $struct_ty: ty,
8484 lifetimes = ( $( $lifetimes: tt) ,* ) ,
8585 ) ,
86- primary_key_field = {
86+ found_fields_with_field_names,
87+ fields = [ $( {
8788 field_name: $field_name: ident,
8889 column_name: $column_name: ident,
8990 field_ty: $field_ty: ty,
9091 field_kind: $field_kind: ident,
9192 $( $rest: tt) *
92- } ,
93+ } ) * ] ,
9394 ) => {
9495 impl <$( $lifetimes) ,* > $crate:: associations:: HasTable for $struct_ty {
9596 type Table = $table_name:: table;
@@ -100,48 +101,28 @@ macro_rules! impl_Identifiable {
100101 }
101102
102103 impl <' ident $( , $lifetimes) * > $crate:: associations:: Identifiable for & ' ident $struct_ty {
103- type Id = & ' ident $field_ty;
104+ type Id = ( $ ( & ' ident $field_ty) , * ) ;
104105
105106 fn id( self ) -> Self :: Id {
106- & self . $field_name
107+ ( $ ( & self . $field_name) , * )
107108 }
108109 }
109110 } ;
110111
111- // Search for the primary key field and continue
112+ // Search for the primary key fields and continue
112113 (
113114 (
114115 table_name = $table_name: ident,
115- primary_key_name = $primary_key_name : ident ,
116+ primary_key_names = $primary_key_names : tt ,
116117 $( $args: tt) *
117118 ) ,
118- fields = [ {
119- field_name: $field_name: ident,
120- $( $rest: tt) *
121- } $( $fields: tt) * ] ,
119+ fields = $fields: tt,
122120 ) => {
123- static_cond! {
124- if $primary_key_name == $field_name {
125- impl_Identifiable! {
126- (
127- table_name = $table_name,
128- $( $args) *
129- ) ,
130- primary_key_field = {
131- field_name: $field_name,
132- $( $rest) *
133- } ,
134- }
135- } else {
136- impl_Identifiable! {
137- (
138- table_name = $table_name,
139- primary_key_name = $primary_key_name,
140- $( $args) *
141- ) ,
142- fields = [ $( $fields) * ] ,
143- }
144- }
121+ __diesel_fields_with_field_names! {
122+ ( table_name = $table_name, $( $args) * ) ,
123+ callback = impl_Identifiable,
124+ targets = $primary_key_names,
125+ fields = $fields,
145126 }
146127 } ;
147128
@@ -345,3 +326,32 @@ fn derive_identifiable_with_non_standard_pk_given_before_table_name() {
345326 assert_eq ! ( & "hi" , foo1. id( ) ) ;
346327 assert_eq ! ( & "there" , foo2. id( ) ) ;
347328}
329+
330+ #[ test]
331+ fn derive_identifiable_with_composite_pk ( ) {
332+ use associations:: Identifiable ;
333+
334+ #[ allow( missing_debug_implementations, missing_copy_implementations, dead_code) ]
335+ struct Foo {
336+ id : i32 ,
337+ foo_id : i32 ,
338+ bar_id : i32 ,
339+ foo : i32 ,
340+ }
341+
342+ impl_Identifiable ! {
343+ #[ primary_key( foo_id, bar_id) ]
344+ #[ table_name( bars) ]
345+ struct Foo {
346+ id: i32 ,
347+ foo_id: i32 ,
348+ bar_id: i32 ,
349+ foo: i32 ,
350+ }
351+ }
352+
353+ let foo1 = Foo { id : 1 , foo_id : 2 , bar_id : 3 , foo : 4 } ;
354+ let foo2 = Foo { id : 5 , foo_id : 6 , bar_id : 7 , foo : 8 } ;
355+ assert_eq ! ( ( & 2 , & 3 ) , foo1. id( ) ) ;
356+ assert_eq ! ( ( & 6 , & 7 ) , foo2. id( ) ) ;
357+ }
0 commit comments