pola-rs/polars · error · DuplicateError
column appears more than once in the query/result cursor
Error message
column {nm!r} appears more than once in the query/result cursor What it means
polars.read_database raises DuplicateError when the cursor's description contains two or more columns with the same name. During result ingestion, _inject_type_overrides walks the cursor description to build schema overrides; a repeated column name would make override/dtype mapping ambiguous, so it fails fast. Fix your SQL to alias columns uniquely.
Solutions
- Alias duplicate columns in the query: SELECT a.id AS a_id, b.id AS b_id
- Reduce the select list to unique columns instead of SELECT *
- Use an explicit schema_overrides/parameter only after deduplicating names — overrides keyed by name cannot disambiguate duplicates
- If the driver allows, fetch by column position and rename in pandas/arrow before conversion
Example fix
// before
pl.read_database("SELECT a.id, b.id FROM a JOIN b ON a.id = b.parent_id", conn)
// after
pl.read_database("SELECT a.id AS a_id, b.id AS parent_id FROM a JOIN b ON a.id = b.parent_id", conn) Defensive patterns
Strategy: validation
Validate before calling
names = [c[0] for c in cur.description]
dupes = {n for n in names if names.count(n) > 1}
if dupes:
raise ValueError(f"duplicate columns in result: {dupes}; alias them in the query") Type guard
def has_unique_columns(cur) -> bool:
names = [d[0] for d in (cur.description or [])]
return len(names) == len(set(names)) Try / catch
from polars.exceptions import DuplicateError
try:
df = pl.read_database(query, conn)
except DuplicateError as e:
# rewrite query with unique aliases
... Prevention
- Alias every joined column explicitly instead of SELECT *
- Lint SQL for duplicate output labels before running
- Test join queries against real schemas in CI
When it happens
Trigger: Calling polars.read_database / pl.read_database with a query whose result set contains duplicate column names, e.g. SELECT a.id, b.id FROM a JOIN b ON ... with no aliases; also possible with SELECT * over tables/views having colliding column names.
Common situations: Joins where both sides expose an identically named column (id, name, created_at); SELECT * from a view combining tables; ORMs or legacy reports emitting duplicate labels.
Understand the failure class
Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.
Related errors
- cannot infer dtype from
- cannot parse input into Polars data type
- cannot return a frame before executing a query
- Cannot set `iter_batches` for
- Cannot set `iter_batches` without also setting a non-zero…
AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18).
Data as JSON: /api/errors/50e529ceb0b56c5d.
Report an issue: GitHub.
Appendix: source
Thrown at py-polars/src/polars/io/database/_executor.py:363
self,
description: list[tuple[str, Any]],
schema_overrides: SchemaDict,
) -> SchemaDict:
"""
Attempt basic dtype inference from a cursor description.
Notes
-----
This is limited; the `type_code` description attr may contain almost anything,
from strings or python types to driver-specific codes, classes, enums, etc.
We currently only do the additional inference from string/python type values.
(Further refinement will require per-driver module knowledge and lookups).
"""
dupe_check = set()
for nm, desc in description:
if nm in dupe_check:
msg = f"column {nm!r} appears more than once in the query/result cursor"
raise DuplicateError(msg)
elif desc is not None and nm not in schema_overrides:
dtype = dtype_from_cursor_description(desc)
if dtype is not None:
schema_overrides[nm] = dtype # type: ignore[index]
dupe_check.add(nm)
return schema_overrides
@staticmethod
def _is_alchemy_async(conn: Any) -> bool:
"""Check if the given connection is SQLALchemy async."""
try:
from sqlalchemy.ext.asyncio import (
AsyncConnection,
AsyncSession,
async_sessionmaker,
)
View on GitHub (pinned to fe841f959e)