update使用map[string]interface{},id主键不生效,造成全表更新 #1700

Closed
opened 2020-06-01 09:57:45 +00:00 by peterwu · 2 comments

使用map[string]interface{}作为更新条件时,ID主键不生效。
affected, err := engine.Table("quick_reply").ID(11).Update(map[string]interface{}{"reply": "hello reply2"}
SQL:UPDATE quick_replySETreply = ? [hello reply2]

源码中:if statement.idParam == nil || statement.RefTable == nil 是有问题的,这边是不是这个判断条件可以考虑进去,如果指定了表名,实际主键ID是可以存在的。

func (statement *Statement) ProcessIDParam() error {
	if statement.idParam == nil || statement.RefTable == nil {
		return nil
	}

	if len(statement.RefTable.PrimaryKeys) != len(statement.idParam) {
		fmt.Println("=====", statement.RefTable.PrimaryKeys, statement.idParam)
		return fmt.Errorf("ID condition is error, expect %d primarykeys, there are %d",
			len(statement.RefTable.PrimaryKeys),
			len(statement.idParam),
		)
	}

	for i, col := range statement.RefTable.PKColumns() {
		var colName = statement.colName(col, statement.TableName())
		statement.cond = statement.cond.And(builder.Eq{colName: statement.idParam[i]})
	}
	return nil
}
使用map[string]interface{}作为更新条件时,ID主键不生效。 affected, err := engine.Table("quick_reply").ID(11).Update(map[string]interface{}{"reply": "hello reply2"} SQL:`UPDATE `quick_reply` SET `reply` = ? [hello reply2]` 源码中:`if statement.idParam == nil || statement.RefTable == nil` 是有问题的,这边是不是这个判断条件可以考虑进去,如果指定了表名,实际主键ID是可以存在的。 ``` func (statement *Statement) ProcessIDParam() error { if statement.idParam == nil || statement.RefTable == nil { return nil } if len(statement.RefTable.PrimaryKeys) != len(statement.idParam) { fmt.Println("=====", statement.RefTable.PrimaryKeys, statement.idParam) return fmt.Errorf("ID condition is error, expect %d primarykeys, there are %d", len(statement.RefTable.PrimaryKeys), len(statement.idParam), ) } for i, col := range statement.RefTable.PKColumns() { var colName = statement.colName(col, statement.TableName()) statement.cond = statement.cond.And(builder.Eq{colName: statement.idParam[i]}) } return nil } ```
Owner

But how do we know which column is the primary key? I think the bug is it should return error but not continue to update.

But how do we know which column is the primary key? I think the bug is it should return error but not continue to update.
lunny added the
kind
bug
label 2020-06-03 01:05:26 +00:00
lunny added this to the 1.0.2 milestone 2020-06-03 01:05:31 +00:00
Author

But how do we know which column is the primary key? I think the bug is it should return error but not continue to update.

it's a solution to prevent a disaster, hope next version,it could be fixed.Thanks.

> But how do we know which column is the primary key? I think the bug is it should return error but not continue to update. it's a solution to prevent a disaster, hope next version,it could be fixed.Thanks.
lunny closed this issue 2020-06-13 01:20:29 +00:00
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#1700
No description provided.