Skip to content

Fix aliases on related entities.#55

Merged
romaninsh merged 21 commits intodevelopfrom
feature/fix-related-entity-aliases
Jul 21, 2016
Merged

Fix aliases on related entities.#55
romaninsh merged 21 commits intodevelopfrom
feature/fix-related-entity-aliases

Conversation

@romaninsh
Copy link
Copy Markdown
Member

When related entity relies on the same table it is possible to run into problem when SQL is confused about which table to use.

select
   name, 
  (select name from item 
   where item.parent_id = item.id
  ) parent_name
from item

To avoid this problem this PR introduces use of aliases in related entities. The related table alias does not have to be passed and it will be automatically deducted from field:

With this patch recursive models produce queries like this:

select
    `pp`.`id`,`pp`.`name`,`pp`.`age`,`pp_i`.`parent_item_id`,
    (select `parent`.`name` 
     from `item` `parent`
     left join `item2` as `parent_i` on `parent_i`.`item_id` = `parent`.`id` 
     where `parent`.`id` = `pp_i`.`parent_item_id`
     ) `parent_item`,
    (select sum(`child`.`age`) from `item` `child`
     left join `item2` as `child_i` on `child_i`.`item_id` = `child`.`id` 
     where `child_i`.`parent_item_id` = `pp`.`id`
    ) `child_age`,`pp`.`id` `_i` 
from `item` `pp`left join `item2` as `pp_i` on `pp_i`.`item_id` = `pp`.`id`

@skondakov
Copy link
Copy Markdown
Contributor

Seems good to me! I've also tested the examples in the section.

There is probably a better way to do this, will fix when we face the
problem.
…data into feature/fix-related-entity-aliases
@romaninsh romaninsh merged commit 74f298f into develop Jul 21, 2016
@romaninsh romaninsh deleted the feature/fix-related-entity-aliases branch July 21, 2016 15:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants