英文:
GORM golang create does not work, getting an mariadb error
问题
我正在尝试将Mariadb集成到我的待办事项应用程序中,因为我正在学习Go语言。我决定使用gorm。我遇到的错误是:
2022/10/16 21:47:49 C:/Users/xxx/go/src/go-todo-app/server/main.go:44 Error 1064: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'RETURNING `id`,`id`' at line 1
[0.000ms] [rows:0] INSERT INTO `todos` (`created_at`,`updated_at`,`deleted_at`,`title`,`done`,`body`) VALUES ('2022-10-16 21:47:49.1','2022-10-16 21:47:49.1',NULL,'Testing',0,'Finish Tutorial') RETURNING `id`,`id`
对于我的HTTP服务器,我正在使用gofiber v2。
app.Post("/api/todos", func(c *fiber.Ctx) error {
todo := &Todo{}
if err := c.BodyParser(todo); err != nil {
return err
}
newTodo := &Todo{
Title: todo.Title,
Body: todo.Body,
Done: 0,
}
db.Create(&newTodo) // 在这里失败
var todos []Todo
db.Find(&todos)
return c.JSON(todos)
})
我的Todo结构如下所示:
type Todo struct {
gorm.Model
ID int `json:"id"`
Title string `json:"title"`
Done int `json:"done"`
Body string `json:"body"`
}
英文:
I'm trying to implement mariadb integration to my todo app, as I'm learning go. I decided to use gorm. The error I'm getting is
2022/10/16 21:47:49 C:/Users/xxx/go/src/go-todo-app/server/main.go:44 Error 1064: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'RETURNING `id`,`id`' at line 1
[0.000ms] [rows:0] INSERT INTO `todos` (`created_at`,`updated_at`,`deleted_at`,`title`,`done`,`body`) VALUES ('2022-10-16 21:47:49.1','2022-10-16 21:47:49.1',NULL,'Testing',0,'Finish Tutorial') RETURNING `id`,`id`
For my http server im using gofiber v2.
app.Post("/api/todos", func(c *fiber.Ctx) error {
todo := &Todo{}
if err := c.BodyParser(todo); err != nil {
return err
}
newTodo := &Todo{
Title: todo.Title,
Body: todo.Body,
Done: 0,
}
db.Create(&newTodo) // fails here
var todos []Todo
db.Find(&todos)
return c.JSON(todos)
})
and my Todo struct looks like this:
type Todo struct {
gorm.Model
ID int `json:"id"`
Title string `json:"title"`
Done int `json:"done"`
Body string `json:"body"`
}
答案1
得分: 3
在您的旧MariaDB版本中明确禁用返回将防止语法错误:
gorm.Open(mysql.New(mysql.Config{Conn: conn, DisableWithReturning: true}))
gorm mysql驱动程序不应该在没有版本检查的情况下启用WithReturning
。这是一个应该报告给hem的错误。
英文:
Explicitly disabling the returning in your older MariaDB version will prevent the syntax error:
gorm.Open(mysql.New(mysql.Config{Conn: conn, DisableWithReturning: true}))
The gorm mysql driver shouldn't be enabling WithReturning
without a version check. This is a bug that should be reported to hem.
答案2
得分: -1
问题已解决。
问题出在我在Windows上使用Xampp安装了MariaDb(PHP 7.4)。在那个版本中,MariaDb不支持RETURNING语句。我按照这个指南安装了MySQL 5.7,代替了MariaDb。https://stackoverflow.com/questions/39654428/how-can-i-change-mariadb-to-mysql-in-xampp
英文:
Problem resolved.
The issue was that I had MariaDb installed with Xampp for windows (PHP 7.4). In that version, RETURNING statement is not available for MariaDb. I followed this guide to install MySQL 5.7 instead of MariaDb. https://stackoverflow.com/questions/39654428/how-can-i-change-mariadb-to-mysql-in-xampp
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论