sea-orm icon indicating copy to clipboard operation
sea-orm copied to clipboard

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

Open Losses opened this issue 1 year ago • 2 comments
trafficstars

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!

Losses avatar Oct 30 '24 10:10 Losses