-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
MySQL insert with JSON doesn't work / ER_WRONG_VALUE_COUNT_ON_ROW #1800
Comments
Check what |
Also the SQL shown in error doesn't seem to be the same that is shown in your example... SourceIP in error is '127.00.93.178' and other values are also different, what is that about? |
@elhigu: This is the output from
As for the values that are different, please ignore them. It was my (bad) attempt at removing any identifying/actual values from the output. So I forgot to make the code example the same. Also, it works fine when I |
Looks like indeed JSON data in mysql https://dev.mysql.com/doc/refman/5.7/en/json.html is just string literal which can be parsed as JSON. IIRC it did work the same way with postgresql where it was necessary to stringify value before inserting... Knex doesn't know anything about the type of column where you are inserting data so it cannot stringify it automatically. E.g. if you have an array type of column JSON-stringifying the passed array would be invalid behavior. There is still one thing that I don't understand... In that error message JSON part seems to be stringified even that it was sent as javascript object to driver... Currently I'm using an ORM, which has actually simple schema for validating columns. That schema is also used to provide info about json columns so ORM can automatically convert JSON column data to string before inserting. |
There is proper support for JSON columns (given that you are using MySQL version 5.7.8+) in latest knex versions, so I am resolving this. |
While this is been marked as resolved, the issue is still there (unless I'm doing something wrong).
migration:
Actual table as it was created in mysql
the insert (the following executes against Postgres without any issues):
query, fished out from
Actual query, which's being executed against mysql server (general_log = 1):
UPDATE. Same issue with |
Thats really really old knex. It shouldn't support json column / dialect version attribute at all. So probably you are really using 0.16.2 or something like that.. This issue only added support that schema allows to create json type of columns. You need to check from mysql driver docs, in which format it requires JSON input to be inserted. Maybe you need to call JSON.stringify() before insert with that. Knex doesn't know nor it cares to which type of column you are inserting stuff. It only passes data that end user is giving to the DB driver. |
Sorry, that was a typo, "1" has "slipped away" from the second offset of the version string to the third.
Yeah, fair enough. That was the reason why I've tried Not a |
I am using I am having same issue. |
The person you are trying to reach is no longer at Just Energy, please redirect your email.
|
This makes perfect sense. However, could Would you accept a PR to do that (and offer some guidance in terms of where this code should go and how the API should look like)? :) |
The person you are trying to reach is no longer at Just Energy, please redirect your email.
|
@jakubka that is task for ORM to implement. It would mean that we need to start add information about tables to knex which would be in fact models. You might be able to tell db driver to do the serialization, but im pretty sure they doesn't support it unless knex would be able to tell column types for the data that is inserted to paramater bindings. So one way to do it could be to add option to insert, which tells column names that should be serialized, but then again... that is as difficult as just do serialization manually. |
Hi,
I'm using knex with MySQL and inserting JSON values but it fails and gives me this error:
Even though if I execute the query generated (logged by knex.js with
debug: true
) directly on MySQL it works without any issues.Sample code:
The text was updated successfully, but these errors were encountered: