问题
Occurs when I run the sequelize-cli command sequelize db:seed:all
When I try and seed an object as JSON I get the following error:
ERROR: Invalid value { viewId: null,
dateRanges: [ { startDate: null, endDate: null } ],
samplingLevel: 'DEFAULT',
dimensions: [ { name: 'ga:channelGrouping' } ],
metrics: [ { expression: 'ga:users' } ] }
This is my model
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;
};
This is my seed file
'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) => {},
};
I was able to insert the same data directly with this query
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');
I wasn't able to find anyone having the same issue as me so I believe it is something simple but I cannot see it.
I'm able to run the seed with no problem if I wrap the object in JSON.stringify() but surely that isn't what is intended.
回答1:
Encountered same problem
Just stringify your query
field like
'{
"viewId": null,
"dateRanges": [
{
"startDate": null,
"endDate": null,
},
],
"samplingLevel": "DEFAULT",
"dimensions": [
{
"name": "ga:channelGrouping",
},
],
"metrics": [{ "expression": "ga:users" }],
}'
@Jaygles take a look at your 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');
you are inserting stringified data. So you need also stringify it in seed
来源:https://stackoverflow.com/questions/49946600/error-seeding-json-data-with-sequelize-into-postgresql-database