在尝试插入与另一个文档有关系的文档时,我遇到了一些麻烦。 API引发了一个例外,没有任何细节。
我收到了以下奇怪的问题:
"_error": {
"code": 422,
"message": "Insertion failure: 1 document(s) contain(s) error(s)"
},
"_issues": {
"exception": "'application'"
},
我使用的是Python 3.4,Eve 0.5.3和MongoDB 3.更多详细信息如下:
架构:
application_schema = {
'label': {
'type': 'string',
'required': True,
'empty': False,
'unique': True,
},
'owner': {
'type': 'string',
'empty': False,
},
}
application = {
'item_title': 'application',
'schema': application_schema
}
delivery_schema = {
'label': {
'type': 'string',
'required': True,
'empty': False,
'unique': True,
},
'app': {
'type': 'objectid',
'required': True,
'data_relation': {
'resource': 'application',
'embeddable': True
},
},
}
delivery = {
'item_title': 'delivery',
'schema': delivery_schema
}
DOMAIN = {
'applications': application,
'deliveries': delivery
}
因此,交付和应用程序之间存在关联。在这里,我将发布一个应用程序(我使用httpie,请参阅https://github.com/jakubroztocil/httpie):
$ http POST :5000/api/applications label="toto"
响应:
HTTP/1.0 201 CREATED
Content-Length: 252
Content-Type: application/json
Date: Wed, 13 May 2015 08:35:14 GMT
Server: Eve/0.5.3 Werkzeug/0.10.4 Python/3.4.3
{
"_created": "2015-05-13",
"_etag": "fc6492cb6ba36424a9e38113026c33e49a60189d",
"_id": "55530cc2962bf270efba95b2",
"_links": {
"self": {
"href": "applications/55530cc2962bf270efba95b2",
"title": "application"
}
},
"_status": "OK",
"_updated": "2015-05-13"
}
现在,如果我尝试使用先前插入的对象中的_id
插入传递,则会引发异常:
$ http POST :5000/api/deliveries label="toto" app="55530cc2962bf270efba95b2"
响应:
HTTP/1.0 422 UNPROCESSABLE ENTITY
Content-Length: 153
Content-Type: application/json
Date: Wed, 13 May 2015 08:39:42 GMT
Server: Eve/0.5.3 Werkzeug/0.10.4 Python/3.4.3
{
"_error": {
"code": 422,
"message": "Insertion failure: 1 document(s) contain(s) error(s)"
},
"_issues": {
"exception": "'application'"
},
"_status": "ERR"
}
答案 0 :(得分:1)
您的data_relation
引用了错误的资源(端点。)
根据您的POST请求(因为您没有发布DOMAIN
定义),您最终定义了applications
和deliveries
作为您的资源,因此您应该参考{{1}您的数据关系中的(复数)。
请尝试以下更新:
applications