Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Embedding only follows _id #1069

Closed
scholzd opened this issue Oct 6, 2017 · 3 comments
Closed

Embedding only follows _id #1069

scholzd opened this issue Oct 6, 2017 · 3 comments

Comments

@scholzd
Copy link

scholzd commented Oct 6, 2017

I have the following schemes:

nodes = {
    'type': 'dict',
    'schema': {
        'hardware': {
            'type': 'string',
            'data_relation': {
                'resource': 'nodes_hw',
                'field': 'name',
                'embeddable': True
            }
        }
    }
}

nodes_hw = {
    'type': 'dict',
    'schema': {
        'name': {
            'type': 'string',
            'required': True,
            'unique': True
        }
    }
}

When now querying

/nodes?embedded={"hardware":1}

the resulting value for hardware is null.

The following lines seem to be the problem

eve/eve/methods/common.py

Lines 826 to 829 in cee2f7e

# NOTE: in case it is a DBRef link, the id_field_name is always the _id
# regardless the Eve set-up
id_field_name = "_id" if isinstance(reference, DBRef) \
else config.DOMAIN[subresource]['id_field']

Internally, in nodes_hw a lookup is performed with _id = VALUE_OF_HARDWARE.

Changing this line to use data_relation['field'] yields the expected result.
Then the internal lookup is correctly performed with name = VALUE_OF_HARDWARE
As result, hardware contains a dictionary with the embedded nodes_hw object.

Am I doing something wrong, is this not wanted or is this a bug?

@Amedeo91
Copy link

Amedeo91 commented Nov 6, 2017

For the timw being, embedded document can be references using the id_field in the resource configuration.

You can use the code in the PR #1033 in order to costonize the research in the "child" document using the hook event.

@scholzd
Copy link
Author

scholzd commented Nov 9, 2017

Thanks for the info!

@einarhuseby
Copy link
Contributor

@nicolaiarocci and @Amedeo91, have been running into a similar case as @scholzd ,and it looks like data_relation["field"] is not honored or am I missing something?

eve/eve/methods/common.py

Lines 905 to 911 in e3838be

# NOTE: in case it is a DBRef link, the id_field_name is always the _id
# regardless the Eve set-up
id_field_name = (
"_id"
if isinstance(reference, DBRef)
else config.DOMAIN[subresource]["id_field"]
)

For now I'm using the following, honoring DBRef and fallback to id_field:

id_field_name = (
    "_id"
    if isinstance(reference, DBRef)
    else data_relation.get("field", False) or config.DOMAIN[subresource]["id_field"]
 )

Or is this unintentionally opening a can of worms?

einarhuseby added a commit to einarhuseby/eve that referenced this issue Jan 26, 2019
einarhuseby added a commit to einarhuseby/eve that referenced this issue Jan 26, 2019
einarhuseby added a commit to einarhuseby/eve that referenced this issue Jan 27, 2019
einarhuseby added a commit to einarhuseby/eve that referenced this issue Jan 27, 2019
einarhuseby added a commit to einarhuseby/eve that referenced this issue Jan 30, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants