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

Issue with select in with_xxx relations #104

Open
anykeyh opened this issue Apr 9, 2019 · 0 comments
Open

Issue with select in with_xxx relations #104

anykeyh opened this issue Apr 9, 2019 · 0 comments

Comments

@anykeyh
Copy link
Owner

anykeyh commented Apr 9, 2019

Issue

Calling with_[relation] method selecting the object without the primary key field in the select will fail as seen in this example:

Example of failure

This will fail:

packages.with_offer(&.select("name")).each ...

giving this error:

[WARN] You're trying to access to the column `id` but it is not initialized.

Here some tips:

  - Ensure that the column `id` exists in your table

  - If the model comes from a collection query, there was maybe a filtering
    on your `select` clause, and you forgot to declare the column `id`

  - In the case of unpersisted models, please initialize by calling `id=`
    first

  - For validator, try `ensure_than` method, or use `id_column.defined?` to
    avoid your validation code.

  - Are you calling `id_column.revert` somewhere before?

  - If your model comes from JSON, please ensure the JSON source defines the
    column. Usage of `strict` mode will trigger exception on JSON loading.

You may want to check the manual:
https://github.com/anykeyh/clear/tree/master/manual/model/Definition.md
https://github.com/anykeyh/clear/tree/master/manual/model/Lifecycle.md

You may also have encountered a bug.
Feel free to submit an issue:
https://github.com/anykeyh/clear/issues/new

Reason

The cache engine use the primary key to encache the relations and avoid N+1. But the primary key is here not accessible.

Fix

Currently a fix seems difficult. It might need to force the selection of the primary key field, but then can lead to difficult situations (e.g. ambigous columns selection).

However, improving the error message might be a first step forward.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant