SeaQL / sea-query

🔱 A dynamic SQL query builder for MySQL, Postgres and SQLite
https://www.sea-ql.org
Other
1.12k stars 182 forks source link

All index hints appear after the last `from` statement #760

Open stepantubanov opened 5 months ago

stepantubanov commented 5 months ago

Description

In a select with multiple from, index hint is simply placed after the last one which produces incorrect SQL.

Steps to Reproduce

fn main() {
    let sql = Query::select()
        .column(Asterisk)
        .from(Alias::new("a"))
        .force_index(IndexName::new("idx_a"), IndexHintScope::All)
        .from(Alias::new("b"))
        .force_index(IndexName::new("idx_b"), IndexHintScope::All)
        .to_string(MysqlQueryBuilder);

    println!("{sql}");
}

Expected Behavior

Correct SQL would be

SELECT * FROM `a` FORCE INDEX (`idx_a`), `b` FORCE INDEX (`idx_b`)

Actual Behavior

Actual generated SQL:

SELECT * FROM `a`, `b` FORCE INDEX (`idx_a`) FORCE INDEX (`idx_b`)

Which results in an error: Key 'idx_a' doesn't exist in table 'b'

Reproduces How Often

Always

Versions

sea-query 0.30.7