Skip to content

Left joins in SQLite can break the query macros #1249

Closed
@ProjectMoon

Description

@ProjectMoon

After some debugging by myself and some people in the #rust:mozilla.org Matrix room, we've come to the conclusion that I've found a bug in SQLx.

If I have two tables like this:

CREATE TABLE IF NOT EXISTS "accounts" (
    "user_id" TEXT PRIMARY KEY NOT NULL UNIQUE,
    "password" TEXT NULL
);

CREATE TABLE IF NOT EXISTS "user_state" (
    "user_id" TEXT PRIMARY KEY NOT NULL UNIQUE, 
    "active_room" TEXT, 
    "account_status" CHECK(account_status IN ('not_registered', 'registered', 'awaiting_activation')) NOT NULL
);

This query will break the query and query_as! macros:

SELECT
    a.user_id as "username",
    a.password,
    s.active_room,
    s.account_status
    FROM accounts a
    LEFT JOIN user_state s on a.user_id = s.user_id
    WHERE a.user_id = 'usernamehere';

The error message returned during compilation is proc macro panicked and help: message: no entry found for key. The code is located at src/sqlite/connection/explain.rs:149:32. This occurs on sqlx 0.5.x, including 0.5.5.

It seems specifically related to the LEFT JOIN. A regular JOIN will compile just fine.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions