javascript - 如何将对象作为数据类型传递给sequelize中的数据模型?

标签 javascript node.js sequelize.js

我需要将一些不是原语的东西传递给数据类型。

我正在构造这样的东西以发送到创建方法:

const p2 = {
          location_model: {
          latitude: lat,
           longitude: lng
      },
        establishment_id: createdEstablishment.id
      }

如您所见,estination_id 是 Integer 类型,但 location_model 是一个对象

这是我的模型

module.exports = {
  up: (queryInterface, DataTypes) => {
    return queryInterface.createTable('EstablishmentLocation', {
      location_model: {
        type: // need some datatype that accept's an object of that type
        allowNull: false,
        references: {
          model: 'Location',
          key: 'id',
        },
      },
      establishment_id: {
        type: DataTypes.INTEGER,
        allowNull: false,
        references: {
          model: 'Establishment',
          key: 'id',
        },
      },
      createdAt: {
        allowNull: false,
        type: DataTypes.STRING,
        defaultValue: DataTypes.STRING,
      },
      updatedAt: {
        allowNull: false,
        type: DataTypes.STRING,
        defaultValue: DataTypes.STRING,
      }
    });
  },

  down: (queryInterface) => {
    return queryInterface.dropTable('EstablishmentLocation');
  }
};

我的问题是,如何将对象传递给 sequilize 中的数据类型?

最佳答案

您在模型中提供的类型是您使用的数据库中定义的类型。如果您使用的是 postgresql,则可以使用 DataTypes.JSON 作为类型。

如果没有,要保存对象,您可以 JSON.stringify 对象并将其作为字符串传递。

关于javascript - 如何将对象作为数据类型传递给sequelize中的数据模型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61282783/

相关文章:

javascript - 如何使用 reqex 将两个特定字符之间的所有出现替换为关联的 char 代码?

node.js - express DELETE 路线表现奇怪?意外 token 'n'

node.js - 如何捕获所有类型的 Sequelize 错误

javascript - Sequelize迁移错误: Cannot read property 'length' of undefined

postgresql - 为什么我的 Sequelize 查询这么慢? (使用 findAll 的嵌套连接)

javascript - 将值设置为来自angularjs Controller 的html标签不起作用

javascript - 来自控制台的警报错误

javascript - HTML 简单下拉列表和结果

node.js - NodeJs命令提示符窗口立即关闭

node.js - Kue 是否使用 Redis 订阅任何 (*) 模式?