42704ERRORTier 2 — Caution✅ HIGH confidenceundefined 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
- 1Referencing a user-defined type, operator, cast, text search configuration, or extension that does not exist
- 2COMMENT ON or GRANT targeting a non-existent object
- 3Using a collation, encoding, or foreign data wrapper name that is not installed
How to reproduce
Referencing a non-existent type.
ALTER TABLE orders ADD COLUMN payload custom_type;
-- custom_type does not existFix 1: Create the type or install the extension before referencing it
When the object is missing.
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 writing migration scripts.
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 docs: https://www.postgresql.org/docs/current/errcodes-appendix.html
🔧 Source ref: Class 42 — Syntax Error or Access Rule Violation
Confidence assessment
✅ HIGH confidence
Standard SQLSTATE for missing object references. Stable across all versions.
See also
🔗 Related errors
📄 Reference pages