Skip to content

duplicate-column

Default severity: error

What it does

Checks that the columns of a query read into a TypedDict, dataclass, Pydantic model or Protocol have distinct names. The message names the projection items that share a name. Tuple and NamedTuple rows, matched by position, are not checked. The ESLint plugin checks every query, since JavaScript drivers return objects.

Why is this bad?

A row factory that maps columns by name (dict_row, class_row, node-postgres) keeps only the last column of a name. SELECT * over a join returns the joined table's id under the key id, silently.

Example

from typing import TypedDict

from sqlty import SQL


class Order(TypedDict):
    id: int


# error: [duplicate-column] "columns `u.id` and `o.id` are both named `id`, and a row mapped by name keeps only the last: alias one, as in `o.id AS o_id`"
q = SQL[Order]("--sql\nSELECT u.id::INTEGER, o.id::INTEGER FROM users u JOIN orders o ON o.user_id = u.id")
Open in playground

Alias one of the columns (o.id AS order_id), or select only the one you need.