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"`
}
CodePudding user response:
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.
CodePudding user response:
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. How can I change MariaDB to MySQL in XAMPP?