我如何在mysql Sequelize实例中具有数组的数据类型? [英] How can I have a datatype of array in mysql Sequelize instance?

查看:1284
本文介绍了我如何在mysql Sequelize实例中具有数组的数据类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个使用Node/Express和MySQL并以Sequelize作为ORM的应用程序.我想使用Array的数据类型,但是续集文档说这仅限于Postgres.

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.

基本上,如果我有一个用户表,该表具有3列(例如,名称,电话,favColors),则我希望favColors填充有从用户检索到的字符串值数组.我该怎么办?

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?

推荐答案

您可以为此使用getter/setter函数:

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(';'));
    },
}

更多信息: http://docs.sequelizejs. com/en/latest/docs/models-definition/#getters-setters

这篇关于我如何在mysql Sequelize实例中具有数组的数据类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆