我是 Sequelize 的新手,来自 MongoDB 和 mongoose,在 mongoose 模式中,您可以这样做以在模式中保存对象字段,
const userSchema = new Schema({
name: String,
age: String,
address: {} // this right here
})
现在我的问题是,如何在 Sequelize 中实现这样的目标?
最佳答案
您可以在 Sequelize 中使用字段的 JSON 数据类型(这对应于 PostgreSQL 中的 JSON 数据类型)。
const User = sequelize.define('User', {
name: {
type: DataTypes.STRING,
allowNull: false
},
age: {
type: DataTypes.STRING
},
address: {
type: DataTypes.JSON
}
}, {
});
这样你就可以向 address
字段写入任何 JS 对象(包括数组)。
关于node.js - 在 Sequelize 中创建对象字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64109332/