Replies: 4 comments 14 replies
-
IIRC, func toDBUserData(ud *data.UserData) *models.UserData {
return &models.UserData{
ID: ud.ID,
Name: ud.Name,
ProvidedData: types.JSON(ud.ProvidedData),
}
} However, instead of passing byte slices around, you can use the system I outlined in this other discussion. |
Beta Was this translation helpful? Give feedback.
-
@stephenafamo Thanks for the quick response! I saw the discussion you link to before I posted. It didn't seem to apply to what I am seeing, but maybe I'm not following all the info provided in that post. The critical part I'm seeing is the failure to insert and update getting error Everything works if I make 2 changes to the generated model. For insert... // COMMENTED OUT generated code
// cache.query = fmt.Sprintf("INSERT INTO `user_data` (`%s`) %%sVALUES (%s)%%s", strings.Join(wl, "`,`"), strmangle.Placeholders(dialect.UseIndexPlaceholders, len(wl), 1, 1))
// TODO: Hardcoded query -- NEED OTHER SOLUTION -- SHOULD NOT UPDATE GENERATED CODE
cache.query = "INSERT INTO `user_data` (`name`,`provided_data`) %sVALUES (?,CONVERT(? using utf8mb4))%s" For update... // COMMENTED OUT generated code
// cache.query = fmt.Sprintf("UPDATE `user_data` SET %s WHERE %s",
// strmangle.SetParamNames("`", "`", 0, wl),
// strmangle.WhereClause("`", "`", 0, userDataPrimaryKeyColumns),
// )
// TODO: Hardcoded query -- NEED OTHER SOLUTION -- SHOULD NOT UPDATE GENERATED CODE
cache.query = "UPDATE `user_data` SET `provided_data`=CONVERT(? using utf8mb4) WHERE `id`=?" |
Beta Was this translation helpful? Give feedback.
-
MySQL installed using
|
Beta Was this translation helpful? Give feedback.
-
For reference for anyone following this thread, here's what I have now with the switch to This change in implementation fixes the issue with insert/update raising errors for the JSON column. But it required changing the database column's type from Skeema definitionNOTE: provided_data changed from type
When the type was set to
Configure remapping of the typeModification: Add
SQL Boiler Generated struct (for reference)Modification: ProvidedData's type changed from type UserData struct {
ID uint64 `boil:"id" json:"id" toml:"id" yaml:"id"`
Name string `boil:"name" json:"name" toml:"name" yaml:"name"`
ProvidedData json.RawMessage `boil:"provided_data" json:"provided_data" toml:"provided_data" yaml:"provided_data"`
R *userDataR `boil:"-" json:"-" toml:"-" yaml:"-"`
L userDataL `boil:"-" json:"-" toml:"-" yaml:"-"`
} My struct for holding the dataModification: ProvidedData's type changed from package data
type UserData struct {
ID uint64
Name string
ProvidedData json.RawMessage // was []byte
} Function to convert from my struct to SQL Boiler structModification: Removed casting for ProvidedData since both structs now have the same datatype for this field. func toDBUserData(ud *data.UserData) *models.UserData {
return &models.UserData{
ID: ud.ID,
Name: ud.Name,
ProvidedData: ud.providedData, // was []byte, but now is json.RawMessage for both structs
}
} |
Beta Was this translation helpful? Give feedback.
-
I am trying to include a JSON column in my table. The JSON is provided by the caller and I have no idea in advance what the data is going to be. I think I am close, but can't seem to get it to work.
What I have so far...
Skeema definition
SQL Boiler Generated struct (for reference)
My struct for holding the data
Function to convert from my struct to SQL Boiler struct
Function to insert
Related suggestion for this error
I found this on stackoverflow which seems to apply (https://stackoverflow.com/questions/38078119/mysql-5-7-12-import-cannot-create-a-json-value-from-a-string-with-character-set).
I changed the
INSERT
statement in the SQLBoiler generated table...FROM:
which generates
TO:
The insert succeeds with this hardcoded
INSERT
statement.Is there another way to accomplish this? I, of course, do not want to commit edits to the generated code.
Beta Was this translation helpful? Give feedback.
All reactions