canonical / sqlair

Friendly type mapping for SQL databases
Apache License 2.0
16 stars 8 forks source link

Selection without using an output argument returns invalid result #154

Open SimonRichardson opened 3 days ago

SimonRichardson commented 3 days ago

The following select statement will return no error, even though the table in question does not have a charm by the name of juju-controller.

Table contents:

uuid                                    name    description     summary subordinate     min_juju_version        run_as_id       assumes lxd_profile
31b7cf74-6773-4bf2-8bf9-ac63cb2947ac    ubuntu  <nil>           <nil>   false           <nil>                   <nil>           <nil>   <nil>

Table schema:

CREATE TABLE charm_run_as_kind (
    id INT PRIMARY KEY,
    name TEXT NOT NULL
);

CREATE UNIQUE INDEX idx_charm_run_as_kind_name
ON charm_run_as_kind (name);

INSERT INTO charm_run_as_kind VALUES
(0, 'default'),
(1, 'root'),
(2, 'sudoer'),
(3, 'non-root');

CREATE TABLE charm (
    uuid TEXT NOT NULL PRIMARY KEY,
    name TEXT,
    description TEXT,
    summary TEXT,
    subordinate BOOLEAN DEFAULT FALSE,
    min_juju_version TEXT,
    run_as_id INT,
    assumes TEXT,
    lxd_profile TEXT,
    CONSTRAINT fk_charm_run_as_kind_charm
    FOREIGN KEY (run_as_id)
    REFERENCES charm_run_as_kind (id)
);

Query that should return sql.ErrNoRows, instead returns nil.

SELECT 1 FROM charm WHERE name = 'juju-controller' AND uuid = $charmID.uuid;
SimonRichardson commented 3 days ago

The work around is to extract something from the table, even if I don't require it.

SELECT name AS &charmName.name
FROM charm
WHERE name = 'juju-controller'
AND uuid = $charmID.uuid;
Aflynn50 commented 3 days ago

Ah yea, this is because if your query has no output expressions in it we do tx.Exec from database/sql under the hood. So no errors get returned.

I've been considering the idea of changing the behvaiour to just do Exec when you do Run and always do tx.Query when you do Get