Skip to content

Commit

Permalink
fix: check if slice is nil when calling BeforeAppendModel
Browse files Browse the repository at this point in the history
  • Loading branch information
vmihailenco committed Dec 29, 2021
1 parent 31e5283 commit 938d9da
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
12 changes: 12 additions & 0 deletions internal/dbtest/orm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,12 @@ func (a Author) String() string {
return fmt.Sprintf("Author<ID=%d Name=%q>", a.ID, a.Name)
}

var _ bun.BeforeAppendModelHook = (*Author)(nil)

func (*Author) BeforeAppendModel(ctx context.Context, query bun.Query) error {
return nil
}

type BookGenre struct {
bun.BaseModel `bun:"alias:bg"` // custom table alias

Expand Down Expand Up @@ -492,6 +498,12 @@ func (b Book) String() string {
return fmt.Sprintf("Book<Id=%d Title=%q>", b.ID, b.Title)
}

var _ bun.BeforeAppendModelHook = (*Book)(nil)

func (*Book) BeforeAppendModel(ctx context.Context, query bun.Query) error {
return nil
}

// BookWithCommentCount is like Book model, but has additional CommentCount
// field that is used to select data into it. The use of `bun:",inherit"` tag
// is essential here so it inherits internal model properties such as table name.
Expand Down
2 changes: 1 addition & 1 deletion model_table_slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (m *sliceTableModel) ScanRows(ctx context.Context, rows *sql.Rows) (int, er
var _ schema.BeforeAppendModelHook = (*sliceTableModel)(nil)

func (m *sliceTableModel) BeforeAppendModel(ctx context.Context, query Query) error {
if !m.table.HasBeforeAppendModelHook() {
if !m.table.HasBeforeAppendModelHook() || !m.slice.IsValid() {
return nil
}

Expand Down

0 comments on commit 938d9da

Please sign in to comment.