1

I have an database column "image" with links to the image of the user on the server. In the next update I need to obtain multiple images, so I'm trying to change the datatype of the column to JSON.

I already put a {" before and "} after the strings. So now the values of the column looks like:

{"/users/57/user-11.png"}

When I try to update the table by:

ALTER TABLE ah_website_dev2.vendor_horses MODIFY COLUMN image json;

I get following error message:

Error Code: 3140. Invalid JSON text: "Missing a colon after a name of object member." at position 27 in value for column '#sql-1d30_44.image'. 0.011 sec
asked Mar 12, 2019 at 8:09
5
  • 1
    Create new field of JSON datatype. Update this field from old (image) field using proper function(s). Check the result. Drop old field, rename new. Commented Mar 12, 2019 at 8:25
  • 1
    {"/users/57/user-11.png"} is invalid JSON Commented Mar 12, 2019 at 8:26
  • @a_horse_with_no_name missing a ";" or something? Commented Mar 12, 2019 at 8:35
  • See The JSON Data Type for correct JSON literal format. Commented Mar 12, 2019 at 8:47
  • is this valid? No. Read user manual please. Commented Mar 12, 2019 at 10:06

2 Answers 2

1

Whilst a JSON field may do what you want, consider proper Data Normalisation rules first.

As soon as you want "more than one" of something, you should be thinking about a new table that contains those multiple instances, with a foreign key back to the table that contained "the one" thing previously.

select * from vendor_horses ; 
+----+---------+
| id | name | 
+----+---------+
| 1 | Red Rum |
+----+---------+
select * from vendor_horse_images ; 
+----+-------+-----------------------+
| id | vh_id | path | 
+----+-------+-----------------------+
| 22 | 1 | /users/57/user-11.png |
| 33 | 1 | /users/57/user-73.png |
+----+-------+-----------------------+
answered Mar 12, 2019 at 11:39
0

{"/users/57/user-11.png"} is not valid json.

You could try something like {"link":"/users/57/user-11.png"}.

However, the json data type looks an odd choice for such values. A correct (and basic) way to do this would be to create a table "links" with one column ID and one string column, and then use vendor_horse_images.path as a FK referencing the path IDs in the new table

answered Mar 21, 2023 at 16:08

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.