@@ -122,12 +122,12 @@ func (s *PersonStore) DebugWith(logger kallax.LoggerFunc) *PersonStore {
122
122
func (s * PersonStore ) relationshipRecords (record * Person ) []kallax.RecordWithSchema {
123
123
var records []kallax.RecordWithSchema
124
124
125
- for _ , rec := range record .Pets {
126
- rec .ClearVirtualColumns ()
127
- rec .AddVirtualColumn ("person_id" , record .GetID ())
125
+ for i := range record .Pets {
126
+ record . Pets [ i ] .ClearVirtualColumns ()
127
+ record . Pets [ i ] .AddVirtualColumn ("person_id" , record .GetID ())
128
128
records = append (records , kallax.RecordWithSchema {
129
129
Schema : Schema .Pet .BaseSchema ,
130
- Record : rec ,
130
+ Record : record . Pets [ i ] ,
131
131
})
132
132
}
133
133
@@ -137,12 +137,10 @@ func (s *PersonStore) relationshipRecords(record *Person) []kallax.RecordWithSch
137
137
// Insert inserts a Person in the database. A non-persisted object is
138
138
// required for this operation.
139
139
func (s * PersonStore ) Insert (record * Person ) error {
140
-
141
140
records := s .relationshipRecords (record )
142
141
143
142
if len (records ) > 0 {
144
143
return s .Store .Transaction (func (s * kallax.Store ) error {
145
-
146
144
if err := s .Insert (Schema .Person .BaseSchema , record ); err != nil {
147
145
return err
148
146
}
@@ -167,7 +165,6 @@ func (s *PersonStore) Insert(record *Person) error {
167
165
}
168
166
169
167
return s .Store .Insert (Schema .Person .BaseSchema , record )
170
-
171
168
}
172
169
173
170
// Update updates the given record on the database. If the columns are given,
@@ -177,12 +174,10 @@ func (s *PersonStore) Insert(record *Person) error {
177
174
// Only writable records can be updated. Writable objects are those that have
178
175
// been just inserted or retrieved using a query with no custom select fields.
179
176
func (s * PersonStore ) Update (record * Person , cols ... kallax.SchemaField ) (updated int64 , err error ) {
180
-
181
177
records := s .relationshipRecords (record )
182
178
183
179
if len (records ) > 0 {
184
180
err = s .Store .Transaction (func (s * kallax.Store ) error {
185
-
186
181
updated , err = s .Update (Schema .Person .BaseSchema , record , cols ... )
187
182
if err != nil {
188
183
return err
@@ -213,7 +208,6 @@ func (s *PersonStore) Update(record *Person, cols ...kallax.SchemaField) (update
213
208
}
214
209
215
210
return s .Store .Update (Schema .Person .BaseSchema , record , cols ... )
216
-
217
211
}
218
212
219
213
// Save inserts the object if the record is not persisted, otherwise it updates
@@ -233,9 +227,7 @@ func (s *PersonStore) Save(record *Person) (updated bool, err error) {
233
227
234
228
// Delete removes the given record from the database.
235
229
func (s * PersonStore ) Delete (record * Person ) error {
236
-
237
230
return s .Store .Delete (Schema .Person .BaseSchema , record )
238
-
239
231
}
240
232
241
233
// Find returns the set of results for the given query.
@@ -641,6 +633,8 @@ func (r *Pet) ColumnAddress(col string) (interface{}, error) {
641
633
return & r .Name , nil
642
634
case "kind" :
643
635
return (* string )(& r .Kind ), nil
636
+ case "person_id" :
637
+ return types .Nullable (kallax .VirtualColumn ("person_id" , r , new (kallax.NumericID ))), nil
644
638
645
639
default :
646
640
return nil , fmt .Errorf ("kallax: invalid column in Pet: %s" , col )
@@ -656,6 +650,8 @@ func (r *Pet) Value(col string) (interface{}, error) {
656
650
return r .Name , nil
657
651
case "kind" :
658
652
return (string )(r .Kind ), nil
653
+ case "person_id" :
654
+ return r .Model .VirtualColumn (col ), nil
659
655
660
656
default :
661
657
return nil , fmt .Errorf ("kallax: invalid column in Pet: %s" , col )
@@ -710,9 +706,7 @@ func (s *PetStore) DebugWith(logger kallax.LoggerFunc) *PetStore {
710
706
// Insert inserts a Pet in the database. A non-persisted object is
711
707
// required for this operation.
712
708
func (s * PetStore ) Insert (record * Pet ) error {
713
-
714
709
return s .Store .Insert (Schema .Pet .BaseSchema , record )
715
-
716
710
}
717
711
718
712
// Update updates the given record on the database. If the columns are given,
@@ -722,9 +716,7 @@ func (s *PetStore) Insert(record *Pet) error {
722
716
// Only writable records can be updated. Writable objects are those that have
723
717
// been just inserted or retrieved using a query with no custom select fields.
724
718
func (s * PetStore ) Update (record * Pet , cols ... kallax.SchemaField ) (updated int64 , err error ) {
725
-
726
719
return s .Store .Update (Schema .Pet .BaseSchema , record , cols ... )
727
-
728
720
}
729
721
730
722
// Save inserts the object if the record is not persisted, otherwise it updates
@@ -744,9 +736,7 @@ func (s *PetStore) Save(record *Pet) (updated bool, err error) {
744
736
745
737
// Delete removes the given record from the database.
746
738
func (s * PetStore ) Delete (record * Pet ) error {
747
-
748
739
return s .Store .Delete (Schema .Pet .BaseSchema , record )
749
-
750
740
}
751
741
752
742
// Find returns the set of results for the given query.
@@ -1095,6 +1085,7 @@ var Schema = &schema{
1095
1085
kallax .NewSchemaField ("id" ),
1096
1086
kallax .NewSchemaField ("name" ),
1097
1087
kallax .NewSchemaField ("kind" ),
1088
+ kallax .NewSchemaField ("person_id" ),
1098
1089
),
1099
1090
ID : kallax .NewSchemaField ("id" ),
1100
1091
Name : kallax .NewSchemaField ("name" ),
0 commit comments