在我运行 sequelize-cli 命令时发生 sequelize db:seed:all
Occurs when I run the sequelize-cli command sequelize db:seed:all
当我尝试将对象作为 JSON 播种时,我收到以下错误:
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' } ] }这是我的模特
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) => {}, };我可以通过这个查询直接插入相同的数据
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.
如果我将对象包装在 JSON.stringify() 中,我可以毫无问题地运行种子,但这肯定不是预期的.
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.
推荐答案遇到同样的问题只需将您的 query 字段字符串化,例如
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 看看你的 sql
@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
更多推荐
使用 sequelize 将 JSON 数据播种到 PostgreSQL 数据库时出错
发布评论