Get()不支持自定义基础类型 #2097

Open
opened 2022-01-11 14:20:46 +00:00 by RelicOfTesla · 5 comments

//正常
id1 := int64(0)
db.Select("ifnull(max(id),0) maxid").Table(Row{}).Get(&id1)


// 失败
type DataId int64
id2 := DataId(0)
db.Select("ifnull(max(id),0) maxid").Table(Row{}).Get(&id2)

出错在xorm v1.2.5 的 AssignValue

之前的v1.0.7 是正常的

```go //正常 id1 := int64(0) db.Select("ifnull(max(id),0) maxid").Table(Row{}).Get(&id1) // 失败 type DataId int64 id2 := DataId(0) db.Select("ifnull(max(id),0) maxid").Table(Row{}).Get(&id2) ``` 出错在xorm v1.2.5 的 AssignValue 之前的v1.0.7 是正常的
lunny added the
kind
bug
label 2022-01-20 13:08:11 +00:00
Owner

What's the error? Please give more details.

What's the error? Please give more details.
lunny added
need
feedback
and removed
kind
bug
labels 2022-01-20 13:23:49 +00:00
Author

package main

import (
	"github.com/stretchr/testify/require"
	"testing"
	"xorm.io/xorm"
)

func testOpen() (*xorm.Engine, error) {
	dsn := "root:123456@tcp(127.0.0.1)/test"
	return xorm.NewEngine("mysql", dsn)
}

type Row struct {
	ID int64 `json:"id" xorm:"id pk autoIncr"`
}

func (Row) TableName() string {
	return "testBaseType"
}

func TestBaseTypeConvert(t *testing.T) {
	must := require.New(t)
	db, err := testOpen()
	must.NoError(err)
	db.ShowSQL(true)

	err = db.Sync2(&Row{})
	must.NoError(err)

	n, err := db.Insert(Row{})
	must.NoError(err)
	must.True(n == 1)

	id1 := int64(0)
	has, err := db.Select("ifnull(max(id),0) maxid").Table(Row{}).Get(&id1)
	must.NoError(err)
	must.True(has)
	must.True(id1 >= 1)

	// 失败
	type DataId int64
	id2 := DataId(0)
	has, err = db.Select("ifnull(max(id),0) maxid").Table(Row{}).Get(&id2)
	must.NoError(err) // converting driver.Value type *int64 to a int64: unsupported value *int64 as int64
	must.True(has)
	must.True(id2 >= 1)

}



```golang package main import ( "github.com/stretchr/testify/require" "testing" "xorm.io/xorm" ) func testOpen() (*xorm.Engine, error) { dsn := "root:123456@tcp(127.0.0.1)/test" return xorm.NewEngine("mysql", dsn) } type Row struct { ID int64 `json:"id" xorm:"id pk autoIncr"` } func (Row) TableName() string { return "testBaseType" } func TestBaseTypeConvert(t *testing.T) { must := require.New(t) db, err := testOpen() must.NoError(err) db.ShowSQL(true) err = db.Sync2(&Row{}) must.NoError(err) n, err := db.Insert(Row{}) must.NoError(err) must.True(n == 1) id1 := int64(0) has, err := db.Select("ifnull(max(id),0) maxid").Table(Row{}).Get(&id1) must.NoError(err) must.True(has) must.True(id1 >= 1) // 失败 type DataId int64 id2 := DataId(0) has, err = db.Select("ifnull(max(id),0) maxid").Table(Row{}).Get(&id2) must.NoError(err) // converting driver.Value type *int64 to a int64: unsupported value *int64 as int64 must.True(has) must.True(id2 >= 1) } ```
RelicOfTesla changed title from AssignValue不支持自定义基础类型 to Get不支持自定义基础类型 2022-11-11 03:27:01 +00:00
RelicOfTesla changed title from Get不支持自定义基础类型 to Get()不支持自定义基础类型 2022-11-11 03:27:24 +00:00
Author
@lunny
lunny added
kind
bug
and removed
need
feedback
labels 2022-11-12 00:52:14 +00:00
lunny closed this issue 2023-07-26 03:03:03 +00:00
Author

XORM v1.3.8: not support uint64/uint32/uint16/uint8 typedef....(btw supported int64/int32)
@lunny

		type DataIdUInt64 uint64
		id_uint64 := DataIdUInt64(0)
		has, err := db.Select("ifnull(max(id),0) maxid").Table(TestBaseType{}).Get(&id_uint64)
		must.NoError(err) // converting driver.Value type *uint64 to a uint64: unsupported value *uint64 as uint64
		must.True(has)
		must.True(id_uint64 >= 1)
XORM v1.3.8: not support uint64/uint32/uint16/uint8 typedef....(btw supported int64/int32) @lunny ```go type DataIdUInt64 uint64 id_uint64 := DataIdUInt64(0) has, err := db.Select("ifnull(max(id),0) maxid").Table(TestBaseType{}).Get(&id_uint64) must.NoError(err) // converting driver.Value type *uint64 to a uint64: unsupported value *uint64 as uint64 must.True(has) must.True(id_uint64 >= 1) ```
Owner

XORM v1.3.8: not support uint64/uint32/uint16/uint8 typedef....(btw supported int64/int32)
@lunny

		type DataIdUInt64 uint64
		id_uint64 := DataIdUInt64(0)
		has, err := db.Select("ifnull(max(id),0) maxid").Table(TestBaseType{}).Get(&id_uint64)
		must.NoError(err) // converting driver.Value type *uint64 to a uint64: unsupported value *uint64 as uint64
		must.True(has)
		must.True(id_uint64 >= 1)

Can you send a PR with this test?

> XORM v1.3.8: not support uint64/uint32/uint16/uint8 typedef....(btw supported int64/int32) > @lunny > ```go > type DataIdUInt64 uint64 > id_uint64 := DataIdUInt64(0) > has, err := db.Select("ifnull(max(id),0) maxid").Table(TestBaseType{}).Get(&id_uint64) > must.NoError(err) // converting driver.Value type *uint64 to a uint64: unsupported value *uint64 as uint64 > must.True(has) > must.True(id_uint64 >= 1) > ``` Can you send a PR with this test?
Sign in to join this conversation.
No Milestone
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: xorm/xorm#2097
No description provided.