0

I try to get values by using xorm and call engine.Find(&xxxx) method but the result object is empty. I've checked that it can get result through engine.Query() method. This means database connection is working correctly and . How to put the result in usertest01?

package main

import (

    "database/sql"
    _ "github.com/mattn/go-oci8"
    "github.com/go-xorm/xorm"
)


type Usertest struct {
    Id       int64  `json:"id" xorm:"'id'"`
    Name     string `json:"name" xorm:"'name'"`
    Password string `json:"password" xorm:"'password'"`
}

var engine *xorm.Engine

func main(){
    engine, err = xorm.NewEngine("oci8", getDSN())
    if err != nil {
        fmt.Println(err.Error())
    }

    var usertest01 []Usertest
    engine.Find(&usertest01)
    fmt.Println("--- result by struct. doesn't work ---")
    fmt.Println(usertest01)

    usertest02, err := engine.Query("select * from usertest")
    fmt.Println("--- result by Query. this works ---")
    fmt.Println(usertest02) 

}

console

[xorm] [info]  2019/04/13 18:24:45.986664 [SQL] SELECT "id", "name", "password" FROM "usertest"
--- result by struct. doesn't work---
[]
[xorm] [info]  2019/04/13 18:24:46.095214 [SQL] select * from usertest
--- result by Query. this works ---
[map[ID:[49] NAME:[78 65 77 69] PASS:[80 65 83 83]]]

O.Takashi
  • 73
  • 2
  • 12
  • Can you share the table definition? Also `Find` returns an `error` value, have you checked whether its `nil` or not? – mkopriva Apr 13 '19 at 14:33
  • ... my guess is that the problem is caused by the letter-case, looking at the column names returned by the `Query` method it looks like you declared the relation in all caps, now when you use identifiers without quotes around them, as you do in `Query`, the matching is *case insensitive*, however when you put quotes around the identifiers, like done by the `Find` method, then the matching is *case sensitive* and since you, most probably, defined the table as `USERTEST` a select query from `"usertest"` cannot possibly succeed since there's no such table. – mkopriva Apr 13 '19 at 14:45
  • 1
    @mkopriva You're right. The table has actual values but the table name is "USERTEST" so that this couldn't find the table. I added the method which returns actual table name and that works. Thanks for your help :) – O.Takashi Apr 14 '19 at 01:23

1 Answers1

1

I added the method which returns the actual table name.

type Usertest struct {
    Id       int64  `json:"id" xorm:"'ID'"`
    Name     string `json:"name" xorm:"'NAME'"`
    Pass string `json:"pass" xorm:"'PASS'"`
}
// add this method
func (Usertest) TableName() string {
    return "USERTEST"
}

main(){
    ...
    ...
    engine.Find(&usertest01)
    fmt.Println(usertest01)
}
O.Takashi
  • 73
  • 2
  • 12