SeaQL / SeaQL/sea-orm

Querying related tables on a linked table fails

Open
#2,959 0 comments 0 reactions 1 assignee View on GitHub

@Huliiiiii is already working on this.

Since Feb 19, 2026.

Dominant language
Rust
Stars
9.9k
Forks
734
Avg merge
6h 36m
Merged PRs (30d)
8

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.