SeaQL / SeaQL/sea-orm

Issue with `cursor_by` and `JOIN` in SeaORM using SQLite

Open
#2,407 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Area:query-builder needs-repro
Dominant language
Rust
Stars
9.9k
Forks
734
Avg merge
6h 36m
Merged PRs (30d)
8

Description

Description

We encountered an issue when using the cursor_by method in SeaORM with a JOIN operation in SQLite. Specifically, when performing a JOIN between two tables (e.g., table A and table B) and using cursor_by on a column from table B, the query fails. The generated SQL incorrectly references the column from table B as if it belongs to table A.

Environment

  • Database: SQLite
  • ORM: SeaORM
  • Version: The issue occurs in version 1.1.0, but not in 0.12.15.

Example

Here's a code snippet illustrating the issue:

let result = media_files::Entity::find()
    .join(
        JoinType::LeftJoin,
        media_file_albums::Relation::MediaFiles.def().rev(),
    )
    .column(media_file_albums::Column::TrackNumber)
    .cursor_by(media_file_albums::Column::TrackNumber)
    .desc()
    .first(20)
    .all(&main_db)
    .await
    .unwrap();
Incorrect SQL Output
SELECT "media_files"."id", "media_files"."file_name", "media_files"."directory", "media_files"."extension", "media_files"."file_hash", "media_files"."last_modified", "media_files"."cover_art_id", "media_files"."sample_rate", "media_files"."duration", "media_file_albums"."track_number" 
FROM "media_files" 
LEFT JOIN "media_file_albums" ON "media_files"."id" = "media_file_albums"."media_file_id" 
ORDER BY "media_files"."track_number" DESC 
LIMIT 20

In this example, track_number is a column from media_file_albums, and we explicitly passed media_file_albums::Column::TrackNumber to cursor_by. However, the generated SQL incorrectly orders by "media_files"."track_number".

Thank you for your attention to this matter!

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.

Research direction

Start by reproducing the provided Rust query with SQLite and inspect how cursor_by builds the ORDER BY clause for a joined column. Compare the generated SQL with the expected table qualification and add regression coverage for this join case. Done means the query orders by media_file_albums.track_number and completes successfully.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust, sqlite
Domain
databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.