PostgreSQL Error 42712: Duplicate Alias

PostgreSQL error 42712 (Duplicate Alias) explained: what it means, why it happens, and how to fix it — with copy-paste code examples.

SQLSTATE 42712 PostgreSQL Last verified 2026-08-26

Quick Answer

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.

Error Code

SQLSTATE: 42712
Official name: Duplicate Alias
System name: duplicate_alias
Service: PostgreSQL

What does this error mean?

The same table alias was specified more than once in the FROM clause of a query.

Exact Error Messages

You may see this error reported as one of the following messages:

Common Causes

How to Fix

  1. Give every FROM/JOIN reference its own alias (u1, u2, ...)
  2. For self-joins, always alias both sides explicitly
  3. Scan the FROM clause for repeated names after editing joins

Code Examples

Failing self-join sql
SELECT *
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.

Fixed with distinct aliases sql
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.

Framework-Specific Fixes

sqlalchemy

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)

You Might Also Like

Frequently Asked Questions

Why am I seeing PostgreSQL error 42712?

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.

How do I fix PostgreSQL error 42712?

Give every FROM/JOIN reference its own alias (u1, u2, ...).

Which frameworks have documented fixes for error 42712?

This page documents fixes for: sqlalchemy.

Official Sources

This page is based on the official PostgreSQL documentation linked below and adds practical troubleshooting guidance on top.