Skip to content
This repository was archived by the owner on Sep 7, 2021. It is now read-only.
This repository is currently being migrated. It's locked while the migration is in progress.

Commit 0902447

Browse files
committed
return error when find with no primary key selected
1 parent 3f2ca6e commit 0902447

File tree

3 files changed

+52
-3
lines changed

3 files changed

+52
-3
lines changed

error.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ var (
2828
ErrConditionType = errors.New("Unsupported condition type")
2929
// ErrUnSupportedSQLType parameter of SQL is not supported
3030
ErrUnSupportedSQLType = errors.New("unsupported sql type")
31+
// ErrNoPrimaryKey represents an error lack of primary key
32+
ErrNoPrimaryKey = errors.New("Current table has no necessary primary key")
33+
// ErrMapKeyIsNotValid represents an error map key is not valid
34+
ErrMapKeyIsNotValid = errors.New("Map key type must be a slice because the table have serval primary keys")
3135
)
3236

3337
// ErrFieldIsNotExist columns does not exist
@@ -49,3 +53,18 @@ type ErrFieldIsNotValid struct {
4953
func (e ErrFieldIsNotValid) Error() string {
5054
return fmt.Sprintf("field %s is not valid on table %s", e.FieldName, e.TableName)
5155
}
56+
57+
// ErrPrimaryKeyNoSelected represents an error primary key not selected
58+
type ErrPrimaryKeyNoSelected struct {
59+
PrimaryKey string
60+
}
61+
62+
func (e ErrPrimaryKeyNoSelected) Error() string {
63+
return fmt.Sprintf("primary key %s is not selected", e.PrimaryKey)
64+
}
65+
66+
// IsErrPrimaryKeyNoSelected returns true is err is ErrPrimaryKeyNoSelected
67+
func IsErrPrimaryKeyNoSelected(err error) bool {
68+
_, ok := err.(ErrPrimaryKeyNoSelected)
69+
return ok
70+
}

session_find.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,10 +250,21 @@ func (session *Session) noCacheFind(table *core.Table, containerValue reflect.Va
250250
} else {
251251
keyType := containerValue.Type().Key()
252252
if len(table.PrimaryKeys) == 0 {
253-
return errors.New("don't support multiple primary key's map has non-slice key type")
253+
return ErrNoPrimaryKey
254254
}
255255
if len(table.PrimaryKeys) > 1 && keyType.Kind() != reflect.Slice {
256-
return errors.New("don't support multiple primary key's map has non-slice key type")
256+
return ErrMapKeyIsNotValid
257+
}
258+
259+
var found bool
260+
for _, field := range fields {
261+
if strings.EqualFold(field, table.PrimaryKeys[0]) {
262+
found = true
263+
break
264+
}
265+
}
266+
if !found {
267+
return ErrPrimaryKeyNoSelected{table.PrimaryKeys[0]}
257268
}
258269

259270
containerValueSetFunc = func(newValue *reflect.Value, pk core.PK) error {

session_find_test.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010
"testing"
1111
"time"
1212

13-
"xorm.io/core"
1413
"github.com/stretchr/testify/assert"
14+
"xorm.io/core"
1515
)
1616

1717
func TestJoinLimit(t *testing.T) {
@@ -801,3 +801,22 @@ func TestFindJoin(t *testing.T) {
801801
Where("scene_item.type=?", 3).Or("device_user_privrels.user_id=?", 339).Find(&scenes)
802802
assert.NoError(t, err)
803803
}
804+
805+
func TestFindMapCols(t *testing.T) {
806+
type FindMapCols struct {
807+
Id int64
808+
ColA string
809+
ColB string
810+
}
811+
812+
assert.NoError(t, prepareEngine())
813+
assertSync(t, new(FindMapCols))
814+
815+
var objs = make(map[int64]*FindMapCols)
816+
err := testEngine.Cols("col_a, col_b").Find(&objs)
817+
assert.Error(t, err)
818+
assert.True(t, IsErrPrimaryKeyNoSelected(err))
819+
820+
err = testEngine.Cols("id, col_a, col_b").Find(&objs)
821+
assert.NoError(t, err)
822+
}

0 commit comments

Comments
 (0)