doumo1807831 2019-04-17 20:04
浏览 161

如何在go-pg(带有Go的Postgres ORM)中的嵌套结构中查询子字段?

I am new to SQL ORM databases and I'm writing code in Golang which works with PostgresQL database. I have this nested structures:

type PersonalInfo struct {
    Name      string `sql:"name"`
    Telephone string `sql:"telephone"`
    Email     string `sql:"email"`
    Age       string `sql:"age"`
    Gender    string `sql:"gender"`
    City      string `sql:"city"`
}

type DB_SessionInfo struct {
    Coockie string
}

type DB_User struct {
    ID           int            `sql:"id, pk"`
    Username     string         `sql:"username,unique"`
    Password     string         `sql:"password"`
    PersonalInfo PersonalInfo   `sql:"personalinfo, type:jsonb"`
    SessionInfo  DB_SessionInfo `sql:"session_info, type:jsonb"`
}

If I'm trying to select item by username, I use the code above and it works well:

func (u *DB_User) GetItemByName(db *pg.DB) error {
    err := db.Model(u).Where("username = ?0", u.Username).Select()
    if err != nil {
        fmt.Printf("Error while getting value by *username*. Reason: %v
", err)
        return err
    }
    fmt.Printf("Get by *username* successful for <%v>
", u)
    return nil
}

Before every request to database i connect to this and create tables by code above:

func someFunction() {
    database := ConnectToDB()
    defer database.Close()

    // some code
    DB_Entry := new(db.DB_User)
    DB_Entry.Username = "Username"

    DB_Entry.GetItemByName(database)
}

func ConnectToDB() *pg.DB {
    database := pg.Connect(&pg.Options{
        User:     "postgres",
        Password: "postgres",
        Database: "postgres",
    })
    fmt.Println("
Successful connection to DB")

    err := createSchema(database)
    if err != nil {
        panic(err)
    } else {
        fmt.Println("Schema created")
    }
    return database
}

func createSchema(database *pg.DB) error {
    for _, model := range []interface{}{(*db.DB_User)(nil), (*db.PersonalInfo)(nil)} {
        err := database.CreateTable(model, &orm.CreateTableOptions{
            IfNotExists: true,
        })
        if err != nil {
            return err
        }
    }
    return nil
}

So, when I want to Select item where personalinfo.name == "some_name" by this code I have error. Code:

func (u *DB_User) AnotherGetItemByName(db *pg.DB) error {
    err := db.Model(u).Where("personalinfo.name = ?0", u.PersonalInfo.Name).Select()
        return err
    }
    return nil
}

Error:

ERROR #42P01 table "personalinfo" doesn`t exist

So my question is, what I am doing wrong? Wrong request in select function? Maybe the mistake in creating tables in function createSchema?

I was trying to find an answer in stackoverflow and reading go-pg documentation, but can't find the solution. I'm sure that has very simple solution and it is basic point of using sql, but i'm new in it. I will be appreciated for help.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥50 永磁型步进电机PID算法
    • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
    • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
    • ¥15 如何处理复杂数据表格的除法运算
    • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
    • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
    • ¥200 uniapp长期运行卡死问题解决
    • ¥15 latex怎么处理论文引理引用参考文献
    • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
    • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?