-
-
Notifications
You must be signed in to change notification settings - Fork 118
Open
Description
Describe the feature
I want query json columns with sql SELECT * FROM `group` WHERE JSON_EXTRACT(`users`,'$') LIKE '%admin%'
But I use datatypes.JSONQuery("files").Likes("%"+user+"%") and get error
Because method not support empty key
Line 188 in 610acc2
| if len(jsonQuery.keys) > 0 { |
package main
import (
"fmt"
"strconv"
"strings"
"gorm.io/datatypes"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
type Group struct {
ID uint `gorm:"primarykey"`
Users datatypes.JSONSlice[string] `gorm:"type:json" `
Name string
}
func main() {
dsn := "root:123456@tcp(localhost:3306)/test_db?charset=utf8mb4&parseTime=True&loc=Local"
db, err := gorm.Open(mysql.New(
mysql.Config{
DSN: dsn,
DefaultStringSize: 256,
DisableDatetimePrecision: true,
DontSupportRenameIndex: true,
DontSupportRenameColumn: true,
SkipInitializeWithVersion: false,
}),
)
if err != nil {
panic(err)
}
var groups []Group
user := "admin"
db.Debug().
// Where(datatypes.JSONQuery("users").Likes("%"+user+"%")). // want to support
Where("JSON_EXTRACT(`users`,'$') LIKE ?", "%"+user+"%"). // current use
Find(&groups)
}
<!-- Describe the requested Feature -->
## Motivation
<!-- Describe the motivation behind it -->
## Related Issues
<!-- Link related issues here -->
Metadata
Metadata
Assignees
Labels
No labels