How to pass object as a data type to a data model in sequilize?(如何将对象作为数据类型传递给 sequilize 中的数据模型?)
问题描述
我需要传递一些不是原始数据类型的东西.
I need to pass something that is not a primitive to the data type.
我正在构造这样的东西来发送到 create 方法:
I am constructing something like this to send to the create method:
const p2 = {
location_model: {
latitude: lat,
longitude: lng
},
establishment_id: createdEstablishment.id
}
如您所见,integer_id 是 Integer 类型,但 location_model 是一个对象
As you can see the establishment_id is of type Integer but the location_model is an object
这是我的模特
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 中将对象传递给数据类型?
My question is, how can I pass an object to a data type in sequilize?
推荐答案
您在模型中提供的 type
是您使用的数据库中定义的类型.如果你使用 postgresql
,你可以使用 DataTypes.JSON
作为类型.
The type
you provide in the model are the types defined in the database you use. If you are using postgresql
, you can use DataTypes.JSON
as the type.
如果没有,要保存对象,您可以JSON.stringify
对象并将其作为字符串传递.
If not, to save object, you can JSON.stringify
the object and pass it as a string.
这篇关于如何将对象作为数据类型传递给 sequilize 中的数据模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何将对象作为数据类型传递给 sequilize 中的数据模型?


基础教程推荐
- 我可以在浏览器中与Babel一起使用ES模块,而不捆绑我的代码吗? 2022-01-01
- html表格如何通过更改悬停边框来突出显示列? 2022-01-01
- 自定义 XMLHttpRequest.prototype.open 2022-01-01
- Electron 将 Node.js 和 Chromium 上下文结合起来意味着 2022-01-01
- Chart.js 在线性图表上拖动点 2022-01-01
- 如何使用JIT在顺风css中使用布局变体? 2022-01-01
- 如何使用TypeScrip将固定承诺数组中的项设置为可选 2022-01-01
- 用于 Twitter 小部件宽度的 HTML/CSS 2022-01-01
- 直接将值设置为滑块 2022-01-01
- Vue 3 – <过渡>渲染不能动画的非元素根节点 2022-01-01