GORM Playground Link
https://gorm.io/docs/connecting_to_the_database.html
-
First, you should change the database's username and password ;
-
And then ,create a database test;
Description
If run this code ,it 's very perfect!
package main
import (
"net/http"
"github.com/gin-gonic/gin"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
type Today struct {
ID int64 `json:"id"`
TodayID int64 `json:"-"`
Name string `json:"name"`
}
type Todo struct {
ID int64 `json:"id"`
Name string `json:"name"`
Todays []*Today `json:"today" gorm:"foreignKey:TodayID;references:ID"`
Class int `json:"class"`
}
type APITodo struct {
ID int64 `json:"id"`
Name string `json:"name"`
Todays []*Today `json:"today" gorm:"foreignKey:TodayID;references:ID"`
}
func main() {
// refer https://github.com/go-sql-driver/mysql#dsn-data-source-name for details
dsn := "root:123@tcp(127.0.0.1:3306)/test?charset=utf8mb4&parseTime=True&loc=Local"
db, _ := gorm.Open(mysql.Open(dsn), &gorm.Config{})
db.AutoMigrate(&Todo{}, &Today{})
r := gin.Default()
r.GET("/", func(c *gin.Context) {
var todoList []APITodo
if err := db.Debug().Model(&Todo{}).Preload("Todays").Find(&todoList).Error; err != nil {
return
}
c.JSON(http.StatusOK, todoList)
})
r.Run(":8090")
}
However,if you just change the order in APITodo , like code below :
package main
import (
"net/http"
"github.com/gin-gonic/gin"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
type Today struct {
ID int64 `json:"id"`
TodayID int64 `json:"-"`
Name string `json:"name"`
}
type Todo struct {
ID int64 `json:"id"`
Name string `json:"name"`
Todays []*Today `json:"today" gorm:"foreignKey:TodayID;references:ID"`
Class int `json:"class"`
}
type APITodo struct {
ID int64 `json:"id"`
Todays []*Today `json:"today" gorm:"foreignKey:TodayID;references:ID"`
Name string `json:"name"`
}
func main() {
// refer https://github.com/go-sql-driver/mysql#dsn-data-source-name for details
dsn := "root:123@tcp(127.0.0.1:3306)/test?charset=utf8mb4&parseTime=True&loc=Local"
db, _ := gorm.Open(mysql.Open(dsn), &gorm.Config{})
db.AutoMigrate(&Todo{}, &Today{})
r := gin.Default()
r.GET("/", func(c *gin.Context) {
var todoList []APITodo
if err := db.Debug().Model(&Todo{}).Preload("Todays").Find(&todoList).Error; err != nil {
return
}
c.JSON(http.StatusOK, todoList)
})
r.Run(":8090")
}
you will find that
2022/03/10 11:50:32 [Recovery] 2022/03/10 - 11:50:32 panic recovered:
GET / HTTP/1.1
Host: 127.0.0.1:8090
Accept: */*
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Cookie: user=MTY0NjQ4OTgzMHxEdi1CQkFFQ180SUFBUkFCRUFBQUhmLUNBQUVHYzNSeWFXNW5EQWdBQm5WelpYSnBaQU5wYm5RRUFnQUN82gIxu1YBKhLf8QOrOVuerheBLJFxOO5PKPPKAWxLkUw=
Postman-Token: 86dd683d-12bb-477a-90aa-640ff9906e83
User-Agent: PostmanRuntime/7.29.0
reflect.Set: value of type []main.Today is not assignable to type string
···
It 's difficult for a “萌新” to find the right way to solve this problem;
I want to use this in a project and it will be more complex;
I will appreciate it if you give me a hand!
Thanks!
Comment From: github-actions[bot]
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨