使用 sequelize 将 JSON 数据播种到 PostgreSQL 数据库时出错

编程入门 行业动态 更新时间:2024-10-21 02:47:39
本文介绍了使用 sequelize 将 JSON 数据播种到 PostgreSQL 数据库时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在我运行 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 数据库时出错

本文发布于:2023-10-13 10:05:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1487647.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据库   数据   sequelize   JSON   PostgreSQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!