我需要将一些不是原语的东西传递给数据类型。
我正在构造这样的东西以发送到创建方法:
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/