How can I have a datatype of array in mysql Sequelize instance?

戏子无情 提交于 2020-01-01 01:13:33

问题


I am building an application that uses Node/Express and MySQL with Sequelize as the ORM. I want to have a datatype of Array, but the sequelize docs says this is limited to postgres only.

Basically, if I have a users table that has 3 columns for example (name, phone, favColors), I want favColors to get populated with an array of string values retrieved from the user. How can I do this?


回答1:


You can use getter/setter functions for this:

favColors: {
    type: Sequelize.STRING,
    allowNull: false,
    get() {
        return this.getDataValue('favColors').split(';')
    },
    set(val) {
       this.setDataValue('favColors',val.join(';'));
    },
}

more info: http://docs.sequelizejs.com/en/latest/docs/models-definition/#getters-setters



来源:https://stackoverflow.com/questions/41860792/how-can-i-have-a-datatype-of-array-in-mysql-sequelize-instance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!