我在 Node.Js/PSQL 项目的 ORM 中使用 Sequalize。我正在创建一个种子文件来预填充我的数据库;但是,当我运行种子文件时,我收到与其中一个字段相关的错误,并包含以下相关信息。使用带有
的 JSON 对象似乎存在问题错误
== 20170308131757-examples: migrating =======
ERROR: Invalid value { example: 'mk@kWO5r' }
定义
const example = sequelize.define('example', {
date: DataTypes.DATE
data: {
type: DataTypes.JSON,
allowNull: true
},
}, {});
种子文件
up: function (queryInterface) {
const examples = [];
examples.push({
date: new Date(),
data: {
data: dataFaker.string({ length: 8 })
},
createdAt: new Date(),
updatedAt: new Date()
});
for(let i = 0; i < 20; i++) {
testAlertLists.push({
date: new Date(),
data: {
data: dataFaker.string({ length: 8 })
},
createdAt: new Date(),
updatedAt: new Date()
});
}
return queryInterface.bulkInsert('examples', examples, {});
},
down: function (queryInterface) {
return queryInterface.bulkDelete('examples', null, {});
}
我保留了相关的代码片段来简化事情,因此如果需要,我可以添加更多字段和其他方面,如果存在与简化事情可能导致的错误无关的小拼写错误,我可以修复如果找到的话。
问题似乎是 Sequalize 不接受插入中的 JSON 对象,但它在代码和表本身中都定义为 JSON 对象(我检查过)。我尝试了几种不同的方式创建 Json,但除此之外我对这个问题有点困惑。将 JSON 对象与 psql 或 Sequalize 一起使用是否存在问题?
最佳答案
Sequelize CLI 存在与 JSON 类型相关的问题。我也遇到了同样的问题,在sequelize-cli的github上找到了解决方案。如果我找到该线程,我将更新源。您需要做的是使用 JSON.stringify()
因为它不会接受该对象。
examples.push({
date: new Date(),
data: JSON.stringify({
data: dataFaker.string({ length: 8 })
}),
createdAt: new Date(),
updatedAt: new Date()
});
关于javascript - 为涉及 DataTypes.JSON 的 Sequelize 创建种子文件时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55149411/