javascript - 如何将对象作为数据类型传递给 sequilize 中的数据模型?
问题描述
我需要将一些不是原始数据类型的东西传递给数据类型。
我正在构建这样的东西来发送到 create 方法:
const p2 = {
location_model: {
latitude: lat,
longitude: lng
},
establishment_id: createdEstablishment.id
}
如您所见,integer_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 中的数据类型?
解决方案
您在模型中提供的type
是您使用的数据库中定义的类型。如果您使用postgresql
,则可以DataTypes.JSON
用作类型。
如果没有,要保存对象,您可以JSON.stringify
将对象作为字符串传递。
推荐阅读
- python - 基于 Django 功能的视图到 FormView
- java - 无法实例化服务,没有零参数构造函数
- java - 如何在 orientdb 3.0 中确保唯一的顶点和边?
- swift - 如何将 SwiftUI ForEach 与字典 [ customEnum : customStrut ] 一起使用 - 出现“符合 'RandomAccessCollection'”错误
- r - 是否有一个 R 函数可以在一个主题中包含混合颜色(列、线、地图......)
- wpf - 更改 observablecollection 中的单元格内容
- json - Fullcalender.io 不接受来自 Nextcloud 的 JSON
- reactjs - 使用新的 auth0 SPA 存储令牌进行 React 和 Redux
- c - Linux:从C代码如何检测哪个eth接口访问WAN?
- ios - 如何快速创建圆形标签?