42704
PostgreSQLERRORNotableSyntax Error or Access Rule ViolationHIGH confidence

undefined object

What this means

SQLSTATE 42704 is raised when a command references a database object (type, operator, cast, function, extension, etc.) that does not exist. It is distinct from 42P01 (undefined table) and is used for non-table objects.

Why it happens
  1. 1Referencing a user-defined type, operator, cast, text search configuration, or extension that does not exist
  2. 2COMMENT ON or GRANT targeting a non-existent object
  3. 3Using a collation, encoding, or foreign data wrapper name that is not installed
How to reproduce

Referencing a non-existent type.

trigger — this will error
trigger — this will error
ALTER TABLE orders ADD COLUMN payload custom_type;
-- custom_type does not exist

expected output

ERROR:  type "custom_type" does not exist

Fix 1

Create the type or install the extension before referencing it

WHEN When the object is missing.

Create the type or install the extension before referencing it
CREATE TYPE custom_type AS (field1 TEXT, field2 INT);
ALTER TABLE orders ADD COLUMN payload custom_type;

Why this works

The referenced object must exist before it can be used. Create it or install the required extension first.

Fix 2

Check for the object in the catalogue before using it

WHEN When writing migration scripts.

Check for the object in the catalogue before using it
SELECT typname FROM pg_type WHERE typname = 'custom_type';

Why this works

Querying pg_type, pg_operator, pg_cast, etc., confirms that the object exists before referencing it in DDL.

Sources
Official documentation ↗

Class 42 — Syntax Error or Access Rule Violation

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All PostgreSQL errors