Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

preload null judgment #4833

Closed
wants to merge 1 commit into from
Closed

preload null judgment #4833

wants to merge 1 commit into from

Conversation

0fv
Copy link
Contributor

@0fv 0fv commented Nov 3, 2021

when using count contain preload,will get panic message

@huacnlee
Copy link
Collaborator

huacnlee commented Nov 4, 2021

Need a test for cover this.

@0fv
Copy link
Contributor Author

0fv commented Nov 4, 2021

Added

@jinzhu
Copy link
Member

jinzhu commented Nov 5, 2021

Hi @0fv

In this case, I think we should return an error, the Preload doesn't works as expected.

@0fv
Copy link
Contributor Author

0fv commented Nov 5, 2021

Hi @0fv

In this case, I think we should return an error, the Preload doesn't works as expected.

In practice,something like page load,sometime need load total count first, when count equal 0,no need call Find function,and after count need add limit and offset get correct page.Here is my example

type Page struct {
	Page  int
	Limit int
	Total int64
}

func (p *Page) LoadPage(db *gorm.DB, data interface{}) error {
	err := db.Count(&p.Total).Error
	if err != nil {
		return err
	}
	if p.Total == 0 {
		return nil
	}
	offset := (p.Page - 1) * p.Limit
	return db.Offset(offset).Limit(p.Limit).Find(data).Error
}

@jinzhu
Copy link
Member

jinzhu commented Nov 29, 2021

maybe consider another way to wrap it, ignore the error silently doesn't sound correct.

@jinzhu jinzhu closed this Nov 29, 2021
@daheige
Copy link
Contributor

daheige commented Nov 29, 2021 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants