diff --git a/error.go b/error.go index 5dc6b84eb..a223fc4a8 100644 --- a/error.go +++ b/error.go @@ -26,10 +26,18 @@ var ( ErrNotImplemented = errors.New("Not implemented") // ErrConditionType condition type unsupported ErrConditionType = errors.New("Unsupported condition type") - // ErrFieldIsNotExist columns does not exist - ErrFieldIsNotExist = errors.New("Field does not exist") ) +// ErrFieldIsNotExist columns does not exist +type ErrFieldIsNotExist struct { + FieldName string + TableName string +} + +func (e ErrFieldIsNotExist) Error() string { + return fmt.Sprintf("field %s is not valid on table %s", e.FieldName, e.TableName) +} + // ErrFieldIsNotValid is not valid type ErrFieldIsNotValid struct { FieldName string diff --git a/session.go b/session.go index 48baf768e..3775eb011 100644 --- a/session.go +++ b/session.go @@ -281,7 +281,7 @@ func (session *Session) doPrepare(db *core.DB, sqlStr string) (stmt *core.Stmt, func (session *Session) getField(dataStruct *reflect.Value, key string, table *core.Table, idx int) (*reflect.Value, error) { var col *core.Column if col = table.GetColumnIdx(key, idx); col == nil { - return nil, ErrFieldIsNotExist + return nil, ErrFieldIsNotExist{key, table.Name} } fieldValue, err := col.ValueOfV(dataStruct) diff --git a/session_find_test.go b/session_find_test.go index 998e54cff..d0ec339cf 100644 --- a/session_find_test.go +++ b/session_find_test.go @@ -770,7 +770,7 @@ func TestFindCacheLimit(t *testing.T) { func TestFindJoin(t *testing.T) { type SceneItem struct { - Type int + Type int DeviceId int64 } @@ -786,4 +786,9 @@ func TestFindJoin(t *testing.T) { err := testEngine.Join("LEFT OUTER", "device_user_privrels", "device_user_privrels.device_id=scene_item.device_id"). Where("scene_item.type=?", 3).Or("device_user_privrels.user_id=?", 339).Find(&scenes) assert.NoError(t, err) + + scenes = make([]SceneItem, 0) + err = testEngine.Join("LEFT OUTER", new(DeviceUserPrivrels), "device_user_privrels.device_id=scene_item.device_id"). + Where("scene_item.type=?", 3).Or("device_user_privrels.user_id=?", 339).Find(&scenes) + assert.NoError(t, err) }