PostgreSQL error 42712 (Duplicate Alias) explained: what it means, why it happens, and how to fix it — with copy-paste code examples.
Give every FROM/JOIN reference its own alias (u1, u2, ...). If that does not apply, for self-joins, always alias both sides explicitly — the full checklist is below.
SQLSTATE: 42712
Official name: Duplicate Alias
System name: duplicate_alias
Service: PostgreSQL
The same table alias was specified more than once in the FROM clause of a query.
You may see this error reported as one of the following messages:
table name "u" specified more than onceSELECT *
FROM users u
JOIN users u ON u.manager_id = u.id; -- 42712
Both sides of the self-join reuse alias u, so PostgreSQL cannot tell them apart.
SELECT e.name, m.name AS manager
FROM users e
JOIN users m ON e.manager_id = m.id;
Distinct aliases e and m make each reference unambiguous.
When building self-joins with the ORM, use aliased() so each reference gets a distinct alias and 42712 cannot occur.
from sqlalchemy.orm import aliased
emp = aliased(User)
mgr = aliased(User)
q = session.query(emp.name, mgr.name).join(mgr, emp.manager_id == mgr.id)
Most often this happens when self-join written without distinct aliases for each reference, or when copy-pasted JOIN clause that repeats the same alias.
Give every FROM/JOIN reference its own alias (u1, u2, ...).
This page documents fixes for: sqlalchemy.
Recommendations are editorial — DB Error Reference takes no payment or affiliate fees for tool listings.
This page is based on the official PostgreSQL documentation linked below and adds practical troubleshooting guidance on top.