Я пытаюсь вставить запись со строкой JSON в качестве значения столбца в MySQL.Значения других столбцов вставлены правильно, но строковый столбец JSON не вставлен.Что мне нужно для этого?
Это фрагмент, где я создаю сущность для сохранения в БД
package main
import (
"encoding/json"
"fmt"
"gocode/os/os-server/model"
)
type Deftheme struct {
AwayIndicator string `json:"awayIndicator"`
ButtonBg string `json:"buttonBg"`
ButtonColor string `json:"bttonColor"`
CenterChannelBg string `json:"centerChannelBg"`
CenterChannelColor string `json:"centerChannelColor"`
CodeTheme string `json:"codeTheme"`
DndIndicator string `json:"dndIndicator"`
ErrorTextColor string `json:"errorTextColor"`
Image string `json:"image"`
LinkColor string `json:"linkColor"`
MentionBg string `json:"mentionBg"`
MentionColor string `json:"mentionColor"`
MentionHighlightBg string `json:"mentionHighlightBg"`
MentionHighlightLink string `json:"mentionHighlightLink"`
NewMessageSeparator string `json:"newMessageSeparator"`
OnlineIndicator string `json:"onlineIndicator"`
SidebarBg string `json:"sidebarBg"`
SidebarHeaderBg string `json:"sidebarHeaderBg"`
SidebarHeaderTextColor string `json:"sidebarHeaderTextColor"`
SidebarText string `json:"sidebarText"`
SidebarTextActiveBorder string `json:"sidebarTextActiveBorder"`
SidebarTextActiveColor string `json:"sidebarTextActiveColor"`
SidebarTextHoverBg string `json:"sidebarTextHoverBg"`
SidebarUnreadText string `json:"sidebarUnreadText"`
Type string `json:"type"`
}
func main() {
m := Deftheme{"#c1b966", "#0177e7", "#ffffff", "#1f1f1f", "#dddddd","monokai","#e81023","#ff6461","/static/files/37bdb7f8db233daef529366b5772bb3f.png","#0d93ff","#0177e7","#ffffff","#784098","#a4ffeb","#cc992d","#399fff","#171717","#1f1f1f","#ffffff","#ffffff","#196caf","#ffffff","#302e30","#ffffff","Windows Dark"}
b,_ := json.Marshal(m)
default_theme_pref := model.Preference{UserId: ruser.Id, Category: model.PREFERENCE_CATEGORY_THEME, Name: '', Value: string(b)}
if err := a.Srv.Store.Preference().Save(&model.Preferences{default_theme_pref}); err != nil {
mlog.Error(fmt.Sprintf("Encountered error saving theme preference, err=%v", err.Message))
}
}
И функцию хранилища, в которой определены функции сохранения
func (s SqlPreferenceStore) Save(preferences *model.Preferences) *model.AppError {
// wrap in a transaction so that if one fails, everything fails
transaction, err := s.GetMaster().Begin()
if err != nil {
return model.NewAppError("SqlPreferenceStore.Save", "store.sql_preference.save.open_transaction.app_error", nil, err.Error(), http.StatusInternalServerError)
}
defer finalizeTransaction(transaction)
for _, preference := range *preferences {
if upsertResult := s.save(transaction, &preference); upsertResult.Err != nil {
return upsertResult.Err
}
}
if err := transaction.Commit(); err != nil {
// don't need to rollback here since the transaction is already closed
return model.NewAppError("SqlPreferenceStore.Save", "store.sql_preference.save.commit_transaction.app_error", nil, err.Error(), http.StatusInternalServerError)
}
return nil
}
func (s SqlPreferenceStore) save(transaction *gorp.Transaction, preference *model.Preference) store.StoreResult {
result := store.StoreResult{}
preference.PreUpdate()
if result.Err = preference.IsValid(); result.Err != nil {
return result
}
params := map[string]interface{}{
"UserId": preference.UserId,
"Category": preference.Category,
"Name": preference.Name,
"Value": preference.Value,
}
if s.DriverName() == model.DATABASE_DRIVER_MYSQL {
if _, err := transaction.Exec(
`INSERT INTO
Preferences
(UserId, Category, Name, Value)
VALUES
(:UserId, :Category, :Name, :Value)
ON DUPLICATE KEY UPDATE
Value = :Value`, params); err != nil {
result.Err = model.NewAppError("SqlPreferenceStore.save", "store.sql_preference.save.updating.app_error", nil, err.Error(), http.StatusInternalServerError)
}
} else {
result.Err = model.NewAppError("SqlPreferenceStore.save", "store.sql_preference.save.missing_driver.app_error", nil, "Failed to update preference because of missing driver", http.StatusNotImplemented)
}
return result
}
Моя структура таблицы выглядит следующим образом:
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| UserId | varchar(26) | NO | PRI | NULL | |
| Category | varchar(32) | NO | PRI | NULL | |
| Name | varchar(32) | NO | PRI | NULL | |
| Value | text | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
Я получаю следующие ошибки
app/user.go:369:102: cannot use '\u0000' (type rune) as type string in field value
app/user.go:369:109: empty character literal or unescaped ' in character literal
app/user.go:369:112: cannot use def_theme_data (type []byte) as type string in field value