I have a model in Sequelize
that communicates with my MySQL
database. The model is defined below.
module.exports= (sequelize, DataTypes)=>{
const Form = sequelize.define(
"form",
{
id:{
type:DataTypes.INTEGER(11).UNSIGNED,
allowNull:false,
autoIncrement:true,
primaryKey:true,
field:"id"
},
name:{
type:DataTypes.STRING(20),
unique:true,
field:"title"
},
createdAt: {
type: DataTypes.DATE,
allowNull: true,
field: "createdAt"
},
updatedAt: {
type: DataTypes.DATE,
allowNull: true,
field: "updatedAt"
},
description:{
type:DataTypes.STRING(300),
allowNull: true,
field:"description"
},
questionId:{
type:DataTypes.ARRAY(DataTypes.INTEGER),
allowNull:true,
field:"questionId"
},
userId_FK:{
type:DataTypes.INTEGER(11).UNSIGNED,
allowNull:false,
field:"userId_FK"
}
},
{
tableName:"form"
}
);
Form.associate = function(models){
Form.belongsTo(models.user,{
foreignKey : "userId_FK",
onDelete : "CASCADE"
});
};
return Form;};
As you can see there is an ARRAY
field in the model defined. How can I add this field in my MySQL
database by using a create table method, eg create table 'form' (....)
.If there is another alternative please let me know.
There is no array field in MySQL, the solution I use is before sending the array to the DB I turn it into string and save it as string in the string field in the DB.
If you are thinking about switching to PostgreSQL there you can use PickleTypes to store data as something simillar to an array.
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.