Skip to content

query_as!/query! inference returns nullable entries on join when a bound parameter is absent #1265

@rtyler

Description

@rtyler

I've been debugging some joins recently and discovered that query_as! has different behavior when there are parameters bound or not.

This example case is with PostgreSQL and I've modified to use query! just so I could see what records were returned. Both macros seem to have the same erroneous behavior.

        let records = sqlx::query!(r#"SELECT schemas.*, shares.name as share_name FROM schemas, shares WHERE share_id = shares.id AND shares.name = $1"#, "rtyler")
            .fetch_all(db)
            .await;

        println!("RECORDS: {:?}", records);
        let records = sqlx::query!(r#"SELECT schemas.*, shares.name as share_name FROM schemas, shares WHERE share_id = shares.id AND shares.name = 'rtyler'"#)
        //let records = sqlx::query!(r#"SELECT schemas.*, shares.name as share_name FROM schemas, shares WHERE share_id = shares.id"#)
            .fetch_all(db)
            .await;

        println!("RECORDS: {:?}", records);

Outputs the following:

 INFO  sqlx::query           > SELECT schemas.*, shares.name as …; rows: 1, elapsed: 3.396ms

SELECT
  schemas.*,
  shares.name as share_name
FROM
  schemas,
  shares
WHERE
  share_id = shares.id
  AND shares.name = $1

RECORDS: Ok([Record { id: 970be392-5de7-479b-a6a0-b027368bcdf8, name: "samples", share_id: fcb12100-2590-496d-9578-d86e2d3ca831, created_at: 2021-05-30T21:52:44.279493Z, share_name: "rtyler" }])
 INFO  sqlx::query           > /* SQLx ping */; rows: 0, elapsed: 309.032µs
 INFO  sqlx::query           > /* SQLx ping */; rows: 0, elapsed: 419.070µs
 INFO  sqlx::query           > SELECT schemas.*, shares.name as …; rows: 1, elapsed: 2.479ms

SELECT
  schemas.*,
  shares.name as share_name
FROM
  schemas,
  shares
WHERE
  share_id = shares.id
  AND shares.name = 'rtyler'

RECORDS: Ok([Record { id: Some(970be392-5de7-479b-a6a0-b027368bcdf8), name: Some("samples"), share_id: Some(fcb12100-2590-496d-9578-d86e2d3ca831), created_at: Some(2021-05-30T21:52:44.279493Z), share_name: Some("rtyler") }])

Something about binding a parameter seems to correct the behavior 😕

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions