-
-
Notifications
You must be signed in to change notification settings - Fork 197
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
updateOrCreate with a JSON column #962
Comments
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Is there a fix for this? The issue persists. |
similar issue - model.merge().save() with a json column is treating the json property like a nested column |
I figured out the issue. It turns out you have to prepare the JSON yourself. Use this in your model (let's say you have a @column({
prepare: value => JSON.stringify(value)
})
declare settings: object If you are using |
If this is the recommended way to serialize JSON, this information ought to go into the documentation. Happy to submit documentation MR, if the maintainers can confirm this is the right way to serialize/deserialize json columns |
The serialization depends on the driver you are using and on the DBMS. For example, nothing has to be done if you are using PSQL. If you want to add something to the documentation, I recommend making it generic and asking people to check how it is done per their DBMS/driver. |
Prerequisites
Hello fellow developers!
I firstly want to thank the adonisjs team for the amazing framework!
I was trying to use the updateOrCreate on a Model, but I got an unexpected error.
When the code triggers the "create", everything works fine and the data is inserted in the database. However, when it triggers the "update", it fails due to a bad conversion of my JSON column (I think).
I get the error (for my mysql database):
As you can see, there are some missing parenthesis around the columns value in err.message . From my understanding, it should be '{"line":true,, ... }' ). I will provide you with my code so you can understand.
I also tried the "update" method but got the same error. I was able to work around this error by using the "update" method on the Database instead of the Model, but had to stringify it myself, otherwise it was failing with the same error.
I could not stringify the columns (formattedColumns) before saving them with the Model because the Model ShowedColumn is expecting a JSON with the type Columns.
It is possible that I am missing something that could be causing this issue I have and that I did not see it.
Thank you for your time!
PS: Can't wait to try the v6!
Package version
"dependencies": {
"@adonisjs/auth": "^8.2.3",
"@adonisjs/core": "^5.9.0",
"@adonisjs/lucid": "^18.4.0",
"@adonisjs/repl": "^3.1.11",
"adonis5-scheduler": "^2.0.2",
"axios": "^1.4.0",
"csvtojson": "^2.0.10",
"ftp": "^0.3.10",
"luxon": "^3.3.0",
"mysql2": "^3.5.0",
"objects-to-csv": "^1.3.6",
"proxy-addr": "^2.0.7",
"reflect-metadata": "^0.1.13",
"source-map-support": "^0.5.21",
"uuid": "^9.0.0",
"workerpool": "^6.4.0"
}
Node.js and npm version
node: v18.16.0
npm: 9.5.1
Sample Code (to reproduce the issue)
Here is my model:
Here is my migration:
And here is where the error occurs:
The text was updated successfully, but these errors were encountered: