[英]How can I querying a subfield in nested structures in go-pg (Postgres ORM with Go)?
I am new to SQL ORM databases and I'm writing code in Golang which works with PostgresQL database.我是 SQL ORM 数据库的新手,我正在用与 PostgresQL 数据库一起使用的 Golang 编写代码。 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:如果我试图通过
username
选择项目,我使用上面的代码并且效果很好:
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\n", err)
return err
}
fmt.Printf("Get by *username* successful for <%v>\n", 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("\nSuccessful 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.因此,当我想通过此代码选择
personalinfo.name == "some_name"
的项目时,出现错误。 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
?也许是在函数
createSchema
中创建表的错误?
I was trying to find an answer in stackoverflow and reading go-pg documentation, but can't find the solution.我试图在 stackoverflow 中找到答案并阅读 go-pg 文档,但找不到解决方案。 I'm sure that has very simple solution and it is basic point of using sql, but i'm new in it.
我确信它有非常简单的解决方案,它是使用 sql 的基本点,但我是新手。 I will be appreciated for help.
我将不胜感激。
You need to tell the ORM to make a JOIN statement on the PersonalInfo table using the Relation
method ( Column
on older versions of go-pg).您需要告诉 ORM 使用
Relation
方法(旧版本的 go-pg 上的Column
)在 PersonalInfo 表上创建 JOIN 语句。
func (u *DB_User) AnotherGetItemByName(db *pg.DB) error {
err := db.Model(u).Relation("PersonalInfo").Where("PersonalInfo.name = ?", u.PersonalInfo.Name).Select()
return err
}
return nil
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.