Skip to content

Commit 4049687

Browse files
committed
Merge branch 'development' of github.com:gobuffalo/pop into development
2 parents d7e5556 + 80b9462 commit 4049687

File tree

8 files changed

+39
-14
lines changed

8 files changed

+39
-14
lines changed

associations/belongs_to_association.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,23 @@ func belongsToAssociationBuilder(p associationParams) (Association, error) {
3131
ownerVal := p.modelValue.FieldByName(p.field.Name)
3232
tags := p.popTags
3333
primaryIDField := defaults.String(tags.Find("primary_id").Value, "ID")
34-
ownerIDField := defaults.String(tags.Find("fk_id").Value, fmt.Sprintf("%s%s", p.field.Name, "ID"))
34+
ownerIDField := fmt.Sprintf("%s%s", p.field.Name, "ID")
35+
36+
if tags.Find("fk_id").Value != "" {
37+
dbTag := tags.Find("fk_id").Value
38+
if _, found := p.modelType.FieldByName(dbTag); !found {
39+
t := p.modelValue.Type()
40+
for i := 0; i < t.NumField(); i++ {
41+
f := t.Field(i)
42+
if f.Tag.Get("db") == dbTag {
43+
ownerIDField = f.Name
44+
break
45+
}
46+
}
47+
} else {
48+
ownerIDField = dbTag
49+
}
50+
}
3551

3652
// belongs_to requires an holding field for the foreign model ID.
3753
if _, found := p.modelType.FieldByName(ownerIDField); !found {

executors_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -972,7 +972,7 @@ func Test_Eager_Create_Belongs_To(t *testing.T) {
972972

973973
car := Taxi{
974974
Model: "Fancy car",
975-
Driver: User{
975+
Driver: &User{
976976
Name: nulls.NewString("Larry 2"),
977977
},
978978
}
@@ -1101,7 +1101,7 @@ func Test_Flat_Create_Belongs_To(t *testing.T) {
11011101

11021102
car := Taxi{
11031103
Model: "Fancy car",
1104-
Driver: user,
1104+
Driver: &user,
11051105
}
11061106

11071107
err = tx.Create(&car)

genny/fizz/ctable/create_table.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func fizzColType(s string) string {
7272
switch strings.ToLower(s) {
7373
case "int":
7474
return "integer"
75-
case "time", "datetime":
75+
case "time.time", "time", "datetime":
7676
return "timestamp"
7777
case "uuid.uuid", "uuid":
7878
return "uuid"

genny/fizz/ctable/create_table_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
func Test_New(t *testing.T) {
1212
r := require.New(t)
1313

14-
ats, err := attrs.ParseArgs("id:uuid", "created_at:timestamp", "updated_at:timestamp", "name", "description:text", "age:int", "bar:nulls.String")
14+
ats, err := attrs.ParseArgs("id:uuid", "created_at:timestamp", "updated_at:timestamp", "name", "description:text", "age:int", "bar:nulls.String", "started_at:time.Time", "finished_at:nulls.Time")
1515
r.NoError(err)
1616

1717
cases := []struct {
@@ -51,6 +51,8 @@ func Test_New(t *testing.T) {
5151
t.Column("description", "text", {})
5252
t.Column("age", "integer", {})
5353
t.Column("bar", "string", {null: true})
54+
t.Column("started_at", "timestamp", {})
55+
t.Column("finished_at", "timestamp", {null: true})
5456
t.Timestamps()
5557
}`,
5658
},

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ module github.com/gobuffalo/pop/v5
33
go 1.13
44

55
require (
6-
github.com/cockroachdb/cockroach-go v0.0.0-20190925194419-606b3d062051
76
github.com/fatih/color v1.9.0
87
github.com/go-sql-driver/mysql v1.5.0
98
github.com/gobuffalo/attrs v0.1.0

pop_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ type Taxi struct {
124124
ID int `db:"id"`
125125
Model string `db:"model"`
126126
UserID nulls.Int `db:"user_id"`
127-
Driver User `belongs_to:"user" fk_id:"UserID"`
127+
Driver *User `belongs_to:"user" fk_id:"user_id"`
128128
CreatedAt time.Time `db:"created_at"`
129129
UpdatedAt time.Time `db:"updated_at"`
130130
}

preload_associations.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -270,11 +270,15 @@ func preloadHasMany(tx *Connection, asoc *AssociationMetaInfo, mmi *ModelMetaInf
270270
asocValue := slice.Elem().Index(i)
271271
if mmi.mapper.FieldByName(mvalue, "ID").Interface() == mmi.mapper.FieldByName(asocValue, foreignField.Path).Interface() ||
272272
reflect.DeepEqual(mmi.mapper.FieldByName(mvalue, "ID"), mmi.mapper.FieldByName(asocValue, foreignField.Path)) {
273-
if modelAssociationField.Kind() == reflect.Slice || modelAssociationField.Kind() == reflect.Array {
273+
274+
switch {
275+
case modelAssociationField.Kind() == reflect.Slice || modelAssociationField.Kind() == reflect.Array:
274276
modelAssociationField.Set(reflect.Append(modelAssociationField, asocValue))
275-
continue
277+
case modelAssociationField.Kind() == reflect.Ptr:
278+
modelAssociationField.Elem().Set(reflect.Append(modelAssociationField.Elem(), asocValue))
279+
default:
280+
modelAssociationField.Set(asocValue)
276281
}
277-
modelAssociationField.Set(asocValue)
278282
}
279283
}
280284
})
@@ -380,11 +384,15 @@ func preloadBelongsTo(tx *Connection, asoc *AssociationMetaInfo, mmi *ModelMetaI
380384
asocValue := slice.Elem().Index(i)
381385
if mmi.mapper.FieldByName(mvalue, fi.Path).Interface() == mmi.mapper.FieldByName(asocValue, "ID").Interface() ||
382386
reflect.DeepEqual(mmi.mapper.FieldByName(mvalue, fi.Path), mmi.mapper.FieldByName(asocValue, "ID")) {
383-
if modelAssociationField.Kind() == reflect.Slice || modelAssociationField.Kind() == reflect.Array {
387+
388+
switch {
389+
case modelAssociationField.Kind() == reflect.Slice || modelAssociationField.Kind() == reflect.Array:
384390
modelAssociationField.Set(reflect.Append(modelAssociationField, asocValue))
385-
continue
391+
case modelAssociationField.Kind() == reflect.Ptr:
392+
modelAssociationField.Elem().Set(asocValue)
393+
default:
394+
modelAssociationField.Set(asocValue)
386395
}
387-
modelAssociationField.Set(asocValue)
388396
}
389397
}
390398
})

soda/cmd/version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
package cmd
22

33
// Version defines the current Pop version.
4-
const Version = "v5.1.0"
4+
const Version = "v5.1.3"

0 commit comments

Comments
 (0)