-
-
Notifications
You must be signed in to change notification settings - Fork 109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
JSONType field Data: define a valid foreign key for relations or implement the Valuer/Scanner #200
Comments
我来看下这个。 |
@liyuan1125 I have test, your code miss 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: 我看下有什么好的设置可以把 JSONType 当作单个 Field 而不当作 Model 去猜测 |
我测了下,可以改进,就是会有点 break change type JSONType[T any] struct {
data T
}
func (j JSONType[T]) GetData() T {
return j.data
} 这样就不会出现上面的 error log 了 |
the fix MR merged, and no error log anymore, but your use
|
The text was updated successfully, but these errors were encountered: