无法创建带有 objectid 和 data_relation 字段的条目

Unable to create an entry with an objectid and data_relation field

我在尝试插入与另一个文档相关的文档时遇到了一些麻烦。 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
}

所以交付和应用程序之间存在关系。在这里,我将 post 一个应用程序(我正在使用 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"
}

您的 data_relation 引用了错误的资源(端点)。

根据您的 POST 请求判断(因为您没有 post 您的 DOMAIN 定义),您最终将 applicationsdeliveries 定义为您的资源,因此您应该在数据关系中引用 applications(复数)。

尝试以下更新:

delivery_schema = {
    'label': {
        'type': 'string',
        'required': True,
        'empty': False,
        'unique': True,
    },
    'app': {
            'type': 'objectid',
            'required': True,
            'data_relation': {
                # replace 'application' with 'applications' as
                # that's the actual endpoint (resource) name.
                'resource': 'applications',
                'embeddable': True
            },
    },
}