GORM Other Playground Link go-gorm/playground#337
SqlServer: scan rows to map[string]interface,type DECIMAL is string and is wrong If you know the table structure and define the structure, the data will be printed correctly. But if the table structure is unknown,then the data will be printed incorrectly.
//**go 1.16.5**
package main
import (
"encoding/json"
"fmt"
"gorm.io/driver/sqlserver"
"gorm.io/gorm"
)
type TEST struct {
Decimals float32 `gorm:"column:decimals;type:decimal(10,4);" json:"decimals"`
}
func main() {
//SQL2016
dsn := fmt.Sprintf("sqlserver://%s:%s@%s:%s?database=%s","sa", "password", "127.0.0.1","1433", "TEST")
db, err := gorm.Open(sqlserver.Open(dsn))
if err != nil {
fmt.Println(err)
}
CreateTable :="IF EXISTS (SELECT * FROM sys.all_objects WHERE object_id = OBJECT_ID(N'[dbo].[test]') AND type IN ('U'))
DROP TABLE [dbo].[test];
CREATE TABLE [dbo].[test] ( [decimals] decimal(10,4) NULL)"
db.Exec(CreateTable)
InsertDate:="INSERT test(decimals)VALUES(1234.5678)"
db.Exec(InsertDate)
// **Know the table structure**
var decimals TEST
db.Raw("Select * from test").Scan(&decimals)
fmt.Println(decimals)
//print: 1234.5677
jsonval1,_:=json.Marshal(decimals)
fmt.Println(string(jsonval1))
//print: {"decimals":1234.5677} right
// **Don't know the table structure**
var data=map[string]interface{}{}
db.Raw("Select * from test").Scan(&data)
fmt.Println(data)
//print: map[decimals:[49 50 51 52 46 53 54 55 56]] wrong
jsonval2,_:=json.Marshal(data)
fmt.Println(string(jsonval2))
//print: {"decimals":"MTIzNC41Njc4"} wrong
}
Comment From: github-actions[bot]
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 2 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨
Comment From: jinzhu
the linked playground PR fixed.
Comment From: lizhenyu0128
GORM Other Playground Link go-gorm/playground#337
SqlServer: scan rows to map[string]interface,type DECIMAL is string and is wrong If you know the table structure and define the structure, the data will be printed correctly. But if the table structure is unknown,then the data will be printed incorrectly.
`` //**go 1.16.5** package main import ( "encoding/json" "fmt" "gorm.io/driver/sqlserver" "gorm.io/gorm" ) type TEST struct { Decimals float32gorm:"column:decimals;type:decimal(10,4);" json:"decimals"` } func main() { //SQL2016 dsn := fmt.Sprintf("sqlserver://%s:%s@%s:%s?database=%s","sa", "password", "127.0.0.1","1433", "TEST") db, err := gorm.Open(sqlserver.Open(dsn)) if err != nil { fmt.Println(err) } CreateTable :="IF EXISTS (SELECT * FROM sys.all_objects WHERE object_id = OBJECT_ID(N'[dbo].[test]') AND type IN ('U')) DROP TABLE [dbo].[test]; CREATE TABLE [dbo].[test] ( [decimals] decimal(10,4) NULL)" db.Exec(CreateTable) InsertDate:="INSERT test(decimals)VALUES(1234.5678)" db.Exec(InsertDate)// Know the table structure var decimals TEST db.Raw("Select * from test").Scan(&decimals) fmt.Println(decimals) //print: 1234.5677
jsonval1,_:=json.Marshal(decimals) fmt.Println(string(jsonval1)) //print: {"decimals":1234.5677} right// Don't know the table structure var data=map[string]interface{}{} db.Raw("Select * from test").Scan(&data) fmt.Println(data) //print: map[decimals:[49 50 51 52 46 53 54 55 56]] wrong jsonval2,_:=json.Marshal(data) fmt.Println(string(jsonval2)) //print: {"decimals":"MTIzNC41Njc4"} wrong } ```
gorm.io/gorm v1.24.0