Update BUG #1599

Closed
opened 2020-03-12 10:16:01 +00:00 by janse_zyd · 8 comments
Contributor
package main

import (
	"fmt"
	"os"

	_ "github.com/mattn/go-sqlite3"
	"xorm.io/xorm"
)

var engine *xorm.Engine

type Test struct {
	ID     string `xorm:"notnull pk"`
	Name   string
	Remark string
}

func main() {
	f := "test.db"
	os.Remove(f)

	orm, err := xorm.NewEngine("sqlite3", f)
	if err != nil {
		fmt.Println(err)
		return
	}
	defer orm.Close()

	orm.ShowSQL(true)

	orm.Sync2(&Test{})

	test := &Test{
		ID:     "123",
		Name:   "name1",
		Remark: "test1",
	}
	_, err = orm.Insert(test)
	if err != nil {
		fmt.Println(err)
		return
	}

	test2 := &Test{}
	_, err = orm.ID(test.ID).Get(test2)
	if err != nil {
		fmt.Println(err)
		return
	}

	fmt.Println("test2 = ", test2)
	test2.Name = "name2"
	test.Remark = "test2"
	num, err := orm.ID(test2.ID).Update(test2)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println("num = ", num)
}
[xorm] [info]  2020/03/12 18:10:34.347575 [SQL] SELECT name FROM sqlite_master WHERE type='table' [] - 862.347µs
[xorm] [info]  2020/03/12 18:10:34.359399 [SQL] CREATE TABLE IF NOT EXISTS `test` (`i_d` TEXT PRIMARY KEY NOT NULL, `name` TEXT NULL, `remark` TEXT NULL) [] - 11.4314ms
[xorm] [info]  2020/03/12 18:10:34.368813 [SQL] INSERT INTO `test` (`i_d`,`name`,`remark`) VALUES (?, ?, ?) [123 name1 test1] - 9.285887ms
[xorm] [info]  2020/03/12 18:10:34.369244 [SQL] SELECT `i_d`, `name`, `remark` FROM `test` WHERE `i_d`=? LIMIT 1 [123] - 73.61µs
test2 =  &{123 name1 test1}
[xorm] [info]  2020/03/12 18:10:34.370252 [SQL] UPDATE `test` SET `name` = ?, `remark` = ? WHERE `i_d`=? [123 name2 test1 123] - 114.232µs
num =  0

好像和ID的定义有关系,改为如下会报错
ID string xorm:"pk"

```go package main import ( "fmt" "os" _ "github.com/mattn/go-sqlite3" "xorm.io/xorm" ) var engine *xorm.Engine type Test struct { ID string `xorm:"notnull pk"` Name string Remark string } func main() { f := "test.db" os.Remove(f) orm, err := xorm.NewEngine("sqlite3", f) if err != nil { fmt.Println(err) return } defer orm.Close() orm.ShowSQL(true) orm.Sync2(&Test{}) test := &Test{ ID: "123", Name: "name1", Remark: "test1", } _, err = orm.Insert(test) if err != nil { fmt.Println(err) return } test2 := &Test{} _, err = orm.ID(test.ID).Get(test2) if err != nil { fmt.Println(err) return } fmt.Println("test2 = ", test2) test2.Name = "name2" test.Remark = "test2" num, err := orm.ID(test2.ID).Update(test2) if err != nil { fmt.Println(err) return } fmt.Println("num = ", num) } ``` ```log [xorm] [info] 2020/03/12 18:10:34.347575 [SQL] SELECT name FROM sqlite_master WHERE type='table' [] - 862.347µs [xorm] [info] 2020/03/12 18:10:34.359399 [SQL] CREATE TABLE IF NOT EXISTS `test` (`i_d` TEXT PRIMARY KEY NOT NULL, `name` TEXT NULL, `remark` TEXT NULL) [] - 11.4314ms [xorm] [info] 2020/03/12 18:10:34.368813 [SQL] INSERT INTO `test` (`i_d`,`name`,`remark`) VALUES (?, ?, ?) [123 name1 test1] - 9.285887ms [xorm] [info] 2020/03/12 18:10:34.369244 [SQL] SELECT `i_d`, `name`, `remark` FROM `test` WHERE `i_d`=? LIMIT 1 [123] - 73.61µs test2 = &{123 name1 test1} [xorm] [info] 2020/03/12 18:10:34.370252 [SQL] UPDATE `test` SET `name` = ?, `remark` = ? WHERE `i_d`=? [123 name2 test1 123] - 114.232µs num = 0 ``` 好像和ID的定义有关系,改为如下会报错 ID string `xorm:"pk"`
Owner

Where is the error?

Where is the error?
Author
Contributor

UPDATE test SET name = ?, remark = ? WHERE i_d=? [123 name2 test1 123]

正确应该是:

UPDATE test SET name = ?, remark = ? WHERE i_d=? [name2 test1 123]

UPDATE `test` SET `name` = ?, `remark` = ? WHERE `i_d`=? [123 name2 test1 123] 正确应该是: UPDATE `test` SET `name` = ?, `remark` = ? WHERE `i_d`=? [name2 test1 123]
Owner

Which version did you use?

Which version did you use?
Author
Contributor

xorm.io/xorm v0.8.3-0.20200311032943-94fd25463869

xorm.io/xorm v0.8.3-0.20200311032943-94fd25463869
Author
Contributor
package main

import (
	"fmt"
	"os"

	_ "github.com/mattn/go-sqlite3"
	"xorm.io/core"
	"xorm.io/xorm"
	"xorm.io/xorm/names"
)

var engine *xorm.Engine

type Test struct {
	ID    string `xorm:"notnull pk" description:"唯一ID号"`
	Name  string `xorm:"notnull pk" description:"名称"`
	Value string `xorm:"notnull varchar(4000)" description:"值"`
}

func main() {
	f := "test.db"
	os.Remove(f)

	orm, err := xorm.NewEngine("sqlite3", f)
	if err != nil {
		fmt.Println(err)
		return
	}
	defer orm.Close()
	orm.SetMapper(names.SameMapper{})

	orm.ShowSQL(true)

	orm.Sync2(&Test{})

	test := &Test{
		ID:    "ID1",
		Name:  "Name1",
		Value: "1",
	}
	_, err = orm.Insert(test)
	if err != nil {
		fmt.Println(err)
		return
	}

	// OK
	test.Value = "2"
	_, err = orm.Where("ID = ? And Name = ?", test.ID, test.Name).Cols("Value").Update(test)
	if err != nil {
		fmt.Println(err)
		return
	}

	// Error 1
	// UPDATE `Test` SET `Value` = ? WHERE (ID = ? And Name = ?) [3 ID1 Name1]
	test.Value = "3"
	num, err := orm.Where("ID = ? And Name = ?", test.ID, test.Name).Update(test)
	if err != nil {
		fmt.Println("error1 = ", err)
	} else {
		// update语句有错误,更新失败
		fmt.Println("num = ", num)
	}

	// Error 2
	test.Value = "4"
	_, err = orm.ID(core.PK{test.ID, test.Name}).Update(test)
	if err != nil {
		fmt.Println("error2 = ", err)
	}
}



// output 

[xorm] [info]  2020/03/13 10:10:50.814754 [SQL] SELECT name FROM sqlite_master WHERE type='table' [] - 789.996µs
[xorm] [info]  2020/03/13 10:10:50.824406 [SQL] CREATE TABLE IF NOT EXISTS `Test` (`ID` TEXT NOT NULL, `Name` TEXT NOT NULL, `Value` TEXT NOT NULL, PRIMARY KEY ( `ID`,`Name` )) [] - 9.051932ms
[xorm] [info]  2020/03/13 10:10:50.830424 [SQL] INSERT INTO `Test` (`ID`,`Name`,`Value`) VALUES (?, ?, ?) [ID1 Name1 1] - 5.792604ms
[xorm] [info]  2020/03/13 10:10:50.836918 [SQL] UPDATE `Test` SET `Value` = ? WHERE (ID = ? And Name = ?) [2 ID1 Name1] - 6.26201ms
[xorm] [info]  2020/03/13 10:10:50.837103 [SQL] UPDATE `Test` SET `Value` = ? WHERE (ID = ? And Name = ?) [ID1 Name1 3 ID1 Name1] - 109.308µs
num =  0
error2 =  ID condition is error, expect 2 primarykeys, there are 1
``` package main import ( "fmt" "os" _ "github.com/mattn/go-sqlite3" "xorm.io/core" "xorm.io/xorm" "xorm.io/xorm/names" ) var engine *xorm.Engine type Test struct { ID string `xorm:"notnull pk" description:"唯一ID号"` Name string `xorm:"notnull pk" description:"名称"` Value string `xorm:"notnull varchar(4000)" description:"值"` } func main() { f := "test.db" os.Remove(f) orm, err := xorm.NewEngine("sqlite3", f) if err != nil { fmt.Println(err) return } defer orm.Close() orm.SetMapper(names.SameMapper{}) orm.ShowSQL(true) orm.Sync2(&Test{}) test := &Test{ ID: "ID1", Name: "Name1", Value: "1", } _, err = orm.Insert(test) if err != nil { fmt.Println(err) return } // OK test.Value = "2" _, err = orm.Where("ID = ? And Name = ?", test.ID, test.Name).Cols("Value").Update(test) if err != nil { fmt.Println(err) return } // Error 1 // UPDATE `Test` SET `Value` = ? WHERE (ID = ? And Name = ?) [3 ID1 Name1] test.Value = "3" num, err := orm.Where("ID = ? And Name = ?", test.ID, test.Name).Update(test) if err != nil { fmt.Println("error1 = ", err) } else { // update语句有错误,更新失败 fmt.Println("num = ", num) } // Error 2 test.Value = "4" _, err = orm.ID(core.PK{test.ID, test.Name}).Update(test) if err != nil { fmt.Println("error2 = ", err) } } // output [xorm] [info] 2020/03/13 10:10:50.814754 [SQL] SELECT name FROM sqlite_master WHERE type='table' [] - 789.996µs [xorm] [info] 2020/03/13 10:10:50.824406 [SQL] CREATE TABLE IF NOT EXISTS `Test` (`ID` TEXT NOT NULL, `Name` TEXT NOT NULL, `Value` TEXT NOT NULL, PRIMARY KEY ( `ID`,`Name` )) [] - 9.051932ms [xorm] [info] 2020/03/13 10:10:50.830424 [SQL] INSERT INTO `Test` (`ID`,`Name`,`Value`) VALUES (?, ?, ?) [ID1 Name1 1] - 5.792604ms [xorm] [info] 2020/03/13 10:10:50.836918 [SQL] UPDATE `Test` SET `Value` = ? WHERE (ID = ? And Name = ?) [2 ID1 Name1] - 6.26201ms [xorm] [info] 2020/03/13 10:10:50.837103 [SQL] UPDATE `Test` SET `Value` = ? WHERE (ID = ? And Name = ?) [ID1 Name1 3 ID1 Name1] - 109.308µs num = 0 error2 = ID condition is error, expect 2 primarykeys, there are 1 ```
lunny added the
kind
bug
label 2020-03-13 02:28:25 +00:00
lunny added this to the 1.0.0 milestone 2020-03-13 02:28:30 +00:00
Owner

Hi, I have sent #1602 to fix this. But and you should change core.PK to schemas.PK even if core.PK will be compitable after #1602 merged.

Hi, I have sent #1602 to fix this. But and you should change `core.PK` to `schemas.PK` even if `core.PK` will be compitable after #1602 merged.
Author
Contributor

第一个问题呢?非core.PK,有解决办法吗?

UPDATE test SET name = ?, remark = ? WHERE i_d=? [123 name2 test1 123]

第一个问题呢?非core.PK,有解决办法吗? UPDATE test SET name = ?, remark = ? WHERE i_d=? [123 name2 test1 123]
lunny closed this issue 2020-03-13 08:57:37 +00:00
Owner

@janse_zyd Please update to latest version and confirm this issue has been resolved.

@janse_zyd Please update to latest version and confirm this issue has been resolved.
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#1599
No description provided.