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

can't scan 1 (float32) into float32 #993

Closed
nasa1024 opened this issue May 30, 2024 · 3 comments
Closed

can't scan 1 (float32) into float32 #993

nasa1024 opened this issue May 30, 2024 · 3 comments
Labels

Comments

@nasa1024
Copy link

what should i fix this error?

  • model
type ScriptCategory struct {
	bun.BaseModel `bun:"script_category,alias:sc" json:"-"` 

	Id         int64   `bun:"id,pk" json:"id"`  
	Name       string  `bun:"name" json:"name"` 
	Sort       float32 `bun:"sort" json:"sort"`
	BaseFields `json:"-"`
}
  • sql
CREATE TABLE `script_category` (
  `id` bigint(20) NOT NULL,
  `name` varchar(255) NOT NULL DEFAULT '',
  `sort` float NOT NULL DEFAULT '0' ,
  `created_at` int(11) unsigned NOT NULL DEFAULT '0' ,
  `updated_at` int(11) unsigned NOT NULL DEFAULT '0' ,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci ;
  • function
func QueryCategory(ctx context.Context, db *bun.DB) (list []model.ScriptCategory, err error) {
	err = db.NewSelect().Model(&model.ScriptCategory{}).
		ColumnExpr("sc.id, sc.name, sc.sort").
		Order("sort asc").
		Scan(ctx, &list)
	if err != nil {
		err = fmt.Errorf("NewSelect ScriptCategory: %v", err)
		return
	}
	return
}
  • error log
[bun]  10:34:57.703   SELECT               19.081ms  SELECT sc.id, sc.name, sc.sort FROM `script_category` AS `sc` ORDER BY `sort` asc 	  *fmt.wrapError: sql: Scan error on column index 2, name "sort": bun: can't scan 1 (float32) into float32 
@CL-Jeremy
Copy link
Contributor

Second this. In addition to this, for me at least, it doesn't work either if I try to scan float32 into float64. I had to effectively leave the field out completely. I'm using Microsoft SQL Server for that matter (I'll have to check which exact version with our IT department). I'd say this much that this is not limited to MySQL.

@CL-Jeremy
Copy link
Contributor

Found out that the root problem in my case actually lies in https://github.com/denisenkom/go-mssqldb.

According to the spec, the scanner function in database/sql should always return the value in double precision, regardless of the precision of the original field. It might be somewhat counter-intuitive (since the logical value of the bits representation does get interpreted differently), but type casts from float32 to float64 then back to float32 retains the precision, so there's nothing wrong with the go spec.

In line 355, this conversion is missing, whereas in line 396 and other cases, the functions comply to the go spec. Adding the needed type cast in line 355 solves my issue.

I suppose that it probably makes more sense to look at https://github.com/go-sql-driver/mysql for your issue.

Copy link

This issue has been automatically marked as stale because it has not had activity in the last 30 days. If there is no update within the next 7 days, this issue will be closed.

@github-actions github-actions bot added the stale label Nov 18, 2024
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Nov 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants