输入数据无效。请验证唯一约束 Strapi

问题描述 投票:0回答:4

我试图在我的 Strapi 产品中添加产品数据,但有时会显示“输入数据无效。请验证唯一约束”错误。有人以前处理过这个吗?

我的控制台中有

{statusCode: 400, error: "Bad Request", message: "Invalid input data. Please verify unique constraints"}

javascript content-management-system strapi
4个回答
0
投票

由于软件包版本不匹配,这发生在我身上。对我有用的最终解决方案是将软件包升级到 3.5.x.

更多细节在这里:https://github.com/strapi/strapi/issues/9369


0
投票

这发生在我身上,因为我的一些模型文件中缺少

const axios = require('axios');
。我自己为每个使用 axios 发送 POST 请求的模型做了 webhooks。

尝试

npm run strapi console
这非常有帮助。当我尝试更新单一类型的字段时,它确实在控制台中显示了一个包含足够信息的错误。

也许你在你的模型/服务/控制器中遗漏了一些东西。希望 Strapi 控制台会显示它。


0
投票

如果这是在生命周期钩子的上下文中,我在 v3 中尝试做一个时遇到了这个错误。

我的错误是没有在 beforeUpdate 方法中添加另一个参数

Before : 
async beforeUpdate(data) {
  console.log(data);
  data.ImageUrlTest = "testurlimage";
},


After:
async beforeUpdate(params, data) {
  console.log(params);
  data.ImageUrlTest = "testurlimage";
},

0
投票

我今天遇到了同样的问题。前端显示的“输入数据无效。请验证唯一约束”似乎是一条包罗万象的消息,并不总是表示存在唯一约束问题。

就我而言,这是因为我试图在特定字段中输入的值比字段的最大长度长。 我们为该字段选择了数据类型

Short text
,但发生错误时我试图保存的特定值是 283 个字符长。

您可能会发现您的问题是类似的数据库错误,也被通用错误消息掩盖了。

我能够通过阅读 Strapi 服务器日志找到这个错误——这揭示了实际的错误消息

value too long for type character varying(255)

1|strapi-dev  | [2023-02-22T19:07:19.844Z] error error: insert into "components_resources_content_resource_sections" ("sectionBodyRichText", "sectionVideoUrl") values ($1, $2) returning * - value too long for type character varying(255)
1|strapi-dev  |     at Parser.parseErrorMessage (/home/ec2-user/content-creation-service/app/node_modules/pg-protocol/dist/parser.js:278:15)
1|strapi-dev  |     at Parser.handlePacket (/home/ec2-user/content-creation-service/app/node_modules/pg-protocol/dist/parser.js:126:29)
1|strapi-dev  |     at Parser.parse (/home/ec2-user/content-creation-service/app/node_modules/pg-protocol/dist/parser.js:39:38)
1|strapi-dev  |     at Socket.<anonymous> (/home/ec2-user/content-creation-service/app/node_modules/pg-protocol/dist/index.js:10:42)
1|strapi-dev  |     at Socket.emit (events.js:315:20)
1|strapi-dev  |     at Socket.EventEmitter.emit (domain.js:486:12)
1|strapi-dev  |     at addChunk (_stream_readable.js:309:12)
1|strapi-dev  |     at readableAddChunk (_stream_readable.js:284:9)
1|strapi-dev  |     at Socket.Readable.push (_stream_readable.js:223:10)
1|strapi-dev  |     at TCP.onStreamRead (internal/stream_base_commons.js:188:23)
1|strapi-dev  | From previous event:
1|strapi-dev  |     at Child.<anonymous> (/home/ec2-user/content-creation-service/app/node_modules/bookshelf/lib/model.js:1160:42)
1|strapi-dev  | From previous event:
1|strapi-dev  |     at Child.<anonymous> (/home/ec2-user/content-creation-service/app/node_modules/bookshelf/lib/model.js:1159:14)
1|strapi-dev  |     at processImmediate (internal/timers.js:461:21)
1|strapi-dev  | From previous event:
1|strapi-dev  |     at Child.<anonymous> (/home/ec2-user/content-creation-service/app/node_modules/bookshelf/lib/model.js:1060:10)
1|strapi-dev  | From previous event:
1|strapi-dev  |     at runCreate (/home/ec2-user/content-creation-service/app/node_modules/strapi-connector-bookshelf/lib/queries.js:113:45)
1|strapi-dev  |     at wrapTransaction (/home/ec2-user/content-creation-service/app/node_modules/strapi-connector-bookshelf/lib/queries.js:52:29)
1|strapi-dev  |     at create (/home/ec2-user/content-creation-service/app/node_modules/strapi-connector-bookshelf/lib/queries.js:120:12)
1|strapi-dev  |     at Object.create (/home/ec2-user/content-creation-service/app/node_modules/strapi-connector-bookshelf/lib/queries.js:58:20)
1|strapi-dev  |     at fn (/home/ec2-user/content-creation-service/app/node_modules/strapi-database/lib/queries/helpers.js:31:54)
1|strapi-dev  |     at Object.create (/home/ec2-user/content-creation-service/app/node_modules/strapi-database/lib/queries/helpers.js:15:24)
1|strapi-dev  |     at async Promise.all (index 0)
1|strapi-dev  |     at async updateComponents (/home/ec2-user/content-creation-service/app/node_modules/strapi-connector-bookshelf/lib/queries.js:408:13)
1|strapi-dev  |     at async runUpdate (/home/ec2-user/content-creation-service/app/node_modules/strapi-connector-bookshelf/lib/queries.js:146:7)
© www.soinside.com 2019 - 2024. All rights reserved.