node.js - 在 Sequelize 中创建对象字段

标签 node.js database postgresql orm sequelize.js

我是 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/

相关文章:

sql - 我怎样才能重写这些子查询

javascript - 从 json 字符串中提取某些列和行

node.js - MongoDB req.body 问题

安卓 SQLite : Replace old database with a new one or use migration scripts

c# - 存储大量分析数据

维护数据库结构的java库

node.js - socket.io 客户端未接收

node.js - 使用 Express.js 显示来自 MongoDB 的日期

mysql - 聊天系统的数据库设计

sql - 使用单个 SQL 关联子查询获取两列