Comments (4)
我来看下这个。
from datatypes.
@liyuan1125 I have test, your code miss db.Model(&TestTable{})
part.
I have create a gist here https://gist.github.com/alingse/adbcd450ea6760bac1d41faa07610870#file-queryjsontype-go-L44-L53 and the log
// ok
row := &TestTable{}
db.Limit(1).Order("id DESC").Find(row)
// find all row &{name 18}
fmt.Println("find all row ", row.Data.Data)
// ok
var field datatypes.JSONType[*User]
err = db.Model(&TestTable{}).Limit(1).Order("id DESC").Pluck("data", &field).Error
// find field &{name 18} <nil>
fmt.Println("find field ", field.Data, err)
// ok
var fullRow TestTable
err = db.Model(&TestTable{}).Limit(1).Order("id DESC").Pluck("data", &fullRow.Data).Error
// find field in row <nil> &{name 18}
fmt.Println("find field in row ", err, fullRow.Data.Data)
it query ok, but really got some error log.
[error] invalid field found for struct gorm.io/datatypes.JSONType[*main.User]'s field Data: define a valid foreign key for relations or implement the Valuer/Scanner interface
find field in row <nil> &{name 18}
查询是正常的,只是会多一些 error log
我感觉是 gorm 首先尝试把 JSONType[*User] 当作 Model 来解析, 尝试这个 Model 的每个 Struct Field,然后失败后再完整当作单个的 field 来查询。
但是我也比较困惑。看调用链路 ,这个 field 的 Datatype 肯定是设置了的,是"json"
但是还是走入了这个分支
if field.DataType == "" && (field.Creatable || field.Updatable || field.Readable) {
if schema.parseRelation(field); schema.err != nil {
return schema, schema.err
目前没有处理 // case guessEmbeddedHas:
所以会有 error log
我看下有什么好的设置可以把 JSONType 当作单个 Field 而不当作 Model 去猜测
from datatypes.
我测了下,可以改进,就是会有点 break change
type JSONType[T any] struct {
data T
}
func (j JSONType[T]) GetData() T {
return j.data
}
这样就不会出现上面的 error log 了
晚点我提交一下 , 之前提交的时候没有测试到 cc @jinzhu
from datatypes.
the fix MR merged, and no error log anymore,
but your Pluck
usage might wrong.
use Pluck
with JSONType[T]
// ok
var field []datatypes.JSONType[*User]
err = db.Model(&TestTable{}).Limit(1).Order("id DESC").Pluck("info", &field).Error
// find field &{name 18} <nil>
fmt.Println("find field ", field[0].Data(), err)
use Pluck
with JSONSlice[T]
// ok
var parents []datatypes.JSONSlice[*User]
err = db.Model(&TestTable{}).Limit(1).Order("id DESC").Pluck("parent", &parents).Error
fmt.Println("find parents in slice ", err, parents[0], *parents[0][0], *parents[0][1])
they both query ok.
see my show case gist https://gist.github.com/alingse/a3187af45d516d591c7a077ed8c739f9
from datatypes.
Related Issues (20)
- how to set json type? HOT 1
- Use civil.Date for date fields HOT 1
- Exclude Unused Imports
- JSONSet support for PostgreSQL
- The JSON_SET feature is already supported and merged, any plans to release a new version?
- 数字数组查询不对劲 HOT 2
- 支持JSON_REMOVE
- Why can't find the function JSONArrayQuery definition? HOT 8
- Found in json.go, but unable to use
- Need Tag for latest PR HOT 2
- Custom enum type sql
- v1.0.7 没有JsonSet功能?
- Please add the JSONArray support for sqlite, thx.
- Is there a solution that allows me to use gob encode / decode with data containing `JsonType` ?
- Greater/Lesser function for JSONQuery
- Add
- Add Enum type support for Postgres
- datatypes.JSONSlice not found HOT 2
- 希望可以支持一下JSONQuery("key").NotEquals(...)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from datatypes.