scribase
SQLSTATE 22001string_data_right_truncationHTTP 400

Postgres error 22001: string data right truncation

value too long for type character varying(n)

A string is longer than the column allows.

Common causes

  • Inserting or updating text longer than a varchar(n) or char(n) limit.
  • Multi-byte characters: the limit counts characters, but an upstream system may have counted bytes.

How to fix it

  • Validate length in the app before writing, with the same limit as the column.
  • If the limit is arbitrary, change the column to text (optionally with a CHECK constraint on length). Changing varchar(n) to text does not rewrite the table.
sql
alter table profiles alter column bio type text;
alter table profiles add constraint bio_length check (char_length(bio) <= 2000);

Through a REST API

PostgREST, the REST layer behind supabase-js and Scribase, answers HTTP 400 for this error and returns the SQLSTATE in the code field of the JSON error body.

Questions

What does Postgres error 22001 mean?

22001 is string_data_right_truncation in class 22 (Data Exception). A string is longer than the column allows.

How do I fix 22001?

Validate length in the app before writing, with the same limit as the column. If the limit is arbitrary, change the column to text (optionally with a CHECK constraint on length). Changing varchar(n) to text does not rewrite the table.

What HTTP status does a REST API return for 22001?

PostgREST, the REST layer behind supabase-js, answers 400.

Other class 22 errors

  • 22003 integer out of range / numeric field overflow
  • 22007 invalid input syntax for type timestamp / date
  • 22008 date/time field value out of range
  • 22012 division by zero
  • 22P02 invalid input syntax for type uuid / integer