undefined object
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.
- 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
Referencing a non-existent type.
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 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.
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.
Class 42 — Syntax Error or Access Rule Violation
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev