简体   繁体   中英

Can not update array field in MYSQL

I am working with MYSQL via Sequelize and I created a field for an array . I read that I could do this with DataTypes of JSON in MYSQL and set the default value to [] . I did this and I would like to update that field with string values. I have tried push like this:

category.dataValues.array_Of_food?.push('item1')

 await category.save()

It won't save the added item in the database. If I use the update method like this:

await category.update({array_Of_food: category.dataValues.category_name})

This changes the array_of_food field with simply a string and not an array. How do I simply push an item into the array field if this is possible?

I solved my problem by using this code:

  category.update({ array_Of_food: Sequelize.fn(
        'JSON_ARRAY_APPEND',
        Sequelize.col('array_of_food'),
        '$',
            food.dataValues.food_Name
      )}) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM