row-type-without-rows¶
Default severity: error
What it does¶
Flags a row type on a statement the database runs without a result set: an INSERT,
UPDATE, DELETE or MERGE without RETURNING, or a schema change such as
CREATE TABLE, ALTER TABLE, DROP or TRUNCATE. SQL[None] and SQL[tuple[()]],
which spell out that no column comes back, are accepted.
schema.sql:
CREATE TABLE users (id SERIAL PRIMARY KEY, name TEXT NOT NULL);
[tool.sqlty]
schema = "schema.sql"
param-types = false
from sqlty import SQL
# error: [row-type-without-rows] "SQL[tuple[int]] should be SQL: the statement returns no rows"
add = SQL[tuple[int]]("--sql\nINSERT INTO users (name) VALUES (%s)")
# error: [row-type-without-rows] "SQL[tuple[int] | None] should be SQL: the statement returns no rows"
copy = SQL[tuple[int] | None]("--sql\nWITH n AS (SELECT 'a' AS name) INSERT INTO users (name) SELECT name FROM n")
added = SQL[tuple[int]]("--sql\nINSERT INTO users (name) VALUES (%s) RETURNING id")
Open in playgroundA driver call that reads rows from such a statement is reported too, without a fix: fetchone,
fetchall or fetchmany chained on execute, a for loop or comprehension over it,
asyncpg's fetch, fetchrow, fetchval and fetchmany, aiosqlite's
execute_fetchall, and the scalar helpers of sqlty.drivers. execute alone, and
rowcount, are fine:
import asyncpg
import psycopg
from sqlty import SQL
conn = psycopg.connect("")
# error: [row-type-without-rows] "`fetchone` reads rows from a statement that returns none"
row = conn.execute("--sql\nINSERT INTO users (name) VALUES (%s)", ("a",)).fetchone()
# error: [row-type-without-rows] "iterates the rows of a statement that returns none"
for gone in conn.execute("--sql\nDELETE FROM users WHERE id = %s", (1,)):
pass
added = conn.execute("--sql\nINSERT INTO users (name) VALUES (%s)", ("a",)).rowcount
new = conn.execute(SQL[tuple[int]]("--sql\nINSERT INTO users (name) VALUES (%s) RETURNING id"), ("a",)).fetchone()
async def main() -> None:
pg = await asyncpg.connect("")
# error: [row-type-without-rows] "`fetch` reads rows from a statement that returns none"
await pg.fetch("--sql\nDELETE FROM users WHERE id = $1", 1)
await pg.execute("--sql\nDELETE FROM users WHERE id = $1", 1)
Open in playgroundWhy is this bad?¶
R promises rows that never come. psycopg's fetchone() raises ProgrammingError: the
last operation didn't produce records, and cursor.description is None. sqlite3 and
asyncpg return no rows, so the code reading them is dead.
Fix¶
sqlty check --fix . drops the row type, or writes None where a parameter type follows
it:
from sqlty import SQL
add = SQL[tuple[int]]("--sql\nINSERT INTO users (name) VALUES (%s)")
named = SQL[tuple[int], tuple[str]]("--sql\nINSERT INTO users (name) VALUES (%s)")
# fixed: from sqlty import SQL
# fixed:
# fixed: add = SQL("--sql\nINSERT INTO users (name) VALUES (%s)")
# fixed: named = SQL[None, tuple[str]]("--sql\nINSERT INTO users (name) VALUES (%s)")
Open in playgroundAdd RETURNING instead to read what the statement wrote. A cursor executed on one line and
fetched on another is not followed.