Fix find with another struct (#1666)

Fix find with another struct

Reviewed-on: https://gitea.com/xorm/xorm/pulls/1666
This commit is contained in:
Lunny Xiao 2020-04-26 11:34:05 +00:00
parent 3bb787a2f7
commit 8ebcb8b557
4 changed files with 13 additions and 3 deletions

View File

@ -708,6 +708,13 @@ func TestFindExtends(t *testing.T) {
err = testEngine.Find(&results) err = testEngine.Find(&results)
assert.NoError(t, err) assert.NoError(t, err)
assert.EqualValues(t, 2, len(results)) assert.EqualValues(t, 2, len(results))
results = make([]FindExtendsA, 0, 2)
err = testEngine.Find(&results, &FindExtendsB{
ID: 1,
})
assert.NoError(t, err)
assert.EqualValues(t, 1, len(results))
} }
func TestFindExtends3(t *testing.T) { func TestFindExtends3(t *testing.T) {

View File

@ -64,7 +64,6 @@ func (statement *Statement) ProcessIDParam() error {
} }
if len(statement.RefTable.PrimaryKeys) != len(statement.idParam) { 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", return fmt.Errorf("ID condition is error, expect %d primarykeys, there are %d",
len(statement.RefTable.PrimaryKeys), len(statement.RefTable.PrimaryKeys),
len(statement.idParam), len(statement.idParam),

View File

@ -108,8 +108,11 @@ func (session *Session) find(rowsSlicePtr interface{}, condiBean ...interface{})
) )
if tp == tpStruct { if tp == tpStruct {
if !session.statement.NoAutoCondition && len(condiBean) > 0 { if !session.statement.NoAutoCondition && len(condiBean) > 0 {
var err error condTable, err := session.engine.tagParser.Parse(reflect.ValueOf(condiBean[0]))
autoCond, err = session.statement.BuildConds(table, condiBean[0], true, true, false, true, addedTableName) if err != nil {
return err
}
autoCond, err = session.statement.BuildConds(condTable, condiBean[0], true, true, false, true, addedTableName)
if err != nil { if err != nil {
return err return err
} }

View File

@ -115,6 +115,7 @@ func (parser *Parser) Parse(v reflect.Value) (*schemas.Table, error) {
t := v.Type() t := v.Type()
if t.Kind() == reflect.Ptr { if t.Kind() == reflect.Ptr {
t = t.Elem() t = t.Elem()
v = v.Elem()
} }
if t.Kind() != reflect.Struct { if t.Kind() != reflect.Struct {
return nil, ErrUnsupportedType return nil, ErrUnsupportedType