简体   繁体   English

将引用不存在元素的元素插入数据库时使用哪个 HTTP 响应状态代码?

[英]Which HTTP response status code to use when inserting an element which references a non existing element into a database?

I have a table with cities that has a foreign key constraint on another table, countries.我有一个包含城市的表,该表在另一个表 countries 上具有外键约束。 What status code should I return if the client sends a POST request with a city that references a country that doesn't exist in the countries table?如果客户端发送的 POST 请求中引用了国家/地区表中不存在的国家/地区,我应该返回什么状态代码? I was thinking 400 BAD REQUEST , but this seems to be related only to the syntax of the request.我在想400 BAD REQUEST ,但这似乎只与请求的语法有关。

404 Not Found would be a better option. 404 Not Found 将是一个更好的选择。 With an error message "country not found".带有错误消息“找不到国家/地区”。

You could use HTTP 422/Unprocessable Entity您可以使用HTTP 422/Unprocessable Entity

The request was well-formed but was unable to be followed due to semantic errors.该请求格式正确,但由于语义错误而无法执行。

The 400/Bad Request is too vague, but 404/Not found could also fit perfectly in your case. 400/Bad Request太模糊了,但是404/Not found也很适合你的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM