运行sequelize-cli命令sequelize db:seed:all
当我尝试将对象播种为JSON时,我收到以下错误:
ERROR: Invalid value { viewId: null,
dateRanges: [ { startDate: null, endDate: null } ],
samplingLevel: 'DEFAULT',
dimensions: [ { name: 'ga:channelGrouping' } ],
metrics: [ { expression: 'ga:users' } ] }
这是我的模特
module.exports = (Sequelize, DataTypes) => {
const Report = Sequelize.define('Report', {
name: {
type: DataTypes.STRING,
allowNull: false,
unique: true,
validate: {
is: /^[a-z0-9\_\-]+$/i,
},
},
platform: {
type: DataTypes.STRING,
allowNull: false,
validate: {
is: /^[a-z0-9\_\-]+$/i,
},
},
query: {
type: DataTypes.JSON,
},
});
return Report;
};
这是我的种子文件
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.bulkInsert('Reports', [
{
name: 'users per channel',
platform: 'google',
query: {
"viewId": null,
"dateRanges": [
{
"startDate": null,
"endDate": null,
},
],
"samplingLevel": "DEFAULT",
"dimensions": [
{
"name": "ga:channelGrouping",
},
],
"metrics": [{ "expression": "ga:users" }],
},
createdAt: new Date(),
updatedAt: new Date(),
},
]);
},
down: (queryInterface, Sequelize) => {},
};
我能够直接使用此查询插入相同的数据
INSERT INTO "Reports" (id, name, platform, query, "createdAt", "updatedAt") VALUES (1, 'users per channel', 'google', '{"viewId":null,"dateRanges":[{"startDate":null,"endDate":null}],"samplingLevel":"DEFAULT","dimensions":[{"name":"ga:channelGrouping"}],"metrics":[{"expression":"ga:users"}]}', '2018-04-15 08:55:12.449-05', '2018-04-15 08:55:12.449-05');
我无法找到任何与我有同样问题的人,所以我相信这很简单,但我看不到它。
如果我将对象包装在JSON.stringify()中,我可以毫无问题地运行种子,但肯定不是预期的。
答案 0 :(得分:0)
遇到同样的问题
只需将query
字段字符串化为
'{
"viewId": null,
"dateRanges": [
{
"startDate": null,
"endDate": null,
},
],
"samplingLevel": "DEFAULT",
"dimensions": [
{
"name": "ga:channelGrouping",
},
],
"metrics": [{ "expression": "ga:users" }],
}'
@Jaygles看一下您的SQL
INSERT INTO "Reports" (id, name, platform, query, "createdAt", "updatedAt") VALUES (1, 'users per channel', 'google', '{"viewId":null,"dateRanges":[{"startDate":null,"endDate":null}],"samplingLevel":"DEFAULT","dimensions":[{"name":"ga:channelGrouping"}],"metrics":[{"expression":"ga:users"}]}', '2018-04-15 08:55:12.449-05', '2018-04-15 08:55:12.449-05');
您要插入字符串化数据。因此,您还需要在种子中对其进行字符串化处理