Skip to content

Querying related tables on a linked table fails #2959

@dsgallups

Description

@dsgallups

Description

When using Select::find_also_linked, SelectTwo::and_also_related does not left join on the aliased name of the linked table. The constructed query at the point of creating SelectTwo does not consider that the previous relationship may have aliased the joined table.

Steps to Reproduce

I think this can be summed up by this, where PostAuthor is Linked<FromEntity = posts::Entity, ToEntity = users::Entity>:

let (post, author, address) = posts::Entity::find_by_id(id)
  .find_also_linked(PostAuthor)
  .and_also_related(address::Entity);

Expected Behavior

I expected to get the related address of the author

Actual Behavior

I believe (but have not verified) that this fails for all database types. For postgres, this error is: invalid reference to FROM-clause entry for table "users"", detail: None, hint: Some("Perhaps you meant to reference the table alias "r0".")

Reproduces How Often

Always except if the table name is r<N> where N is a zero-indexed count of linked aliases, and the table's name lines up exactly with the position of its alias.

Workarounds

  1. Creating a FromQueryResult type (in 1.x). Unsure for 2.x
  2. Loading the related table with LoaderTrait in v1

Versions

v1. I snooped around to discover that the same issue exists in v2 in src/query/join.rs:253.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions