unknown-table¶
Default severity: error
What it does¶
Reports a query that reads a table the configured schema does not define: a FROM or JOIN item, a DML target with RETURNING, or a qualifier (x.id, x.*) that names no table or alias in the query.
[tool.sqlty]
schema = "schema.sql"
[tool.sqlty.rules]
unknown-table = "warn"
schema.sql:
CREATE TABLE users (id INTEGER, name TEXT);
from sqlty import SQL
# error: [unknown-table] "table `user` is not defined in the schema"
q = SQL[tuple[int]]("--sql\nSELECT id FROM user")
Open in playgroundWhy is this bad?¶
A column of an unknown table has no type, so the annotation of the row is not checked. A typo in a table name, or a table missing from the schema dump, hides every column below it.
How to fix¶
Fix the name, or add the table to the schema file. CTEs, derived tables, views in the schema, table functions (generate_series, unnest), PostgreSQL system catalogs (pg_catalog, information_schema, pg_*) and DML FROM/USING sources are not reported. To accept one case, put # sqlty: ignore[unknown-table] on its line.
Configuration¶
An error by default: the database rejects such SQL. Lower or turn it off:
[tool.sqlty.rules]
unknown-table = "warn"
Only checked when a schema with tables is configured: without one every table is unknown, so the rule stays silent. Applies to the postgres, mysql and sqlite dialects. Case is folded as the database does, so Users and users are one table.