4

While inserting data into a table which has many columns:

INSERT INTO MyTable ("name", ..100+ columns)
 VALUES ('Michel', ... 100+ values)

I made an error creating a specific value so PostgreSQL tells us:

ERROR:  value too long for type character varying(2)

I would like to avoid going through the whole table schema to guess which column is failing.

Is there a way to configure PostgreSQL so it tells us which column is causing the error?

1 Answer 1

1

One quick way might be to query the information schema table for your database and look for character columns having a maximum width of 2 (to which your error is alluding):

SELECT column_name
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'MyTable' AND
      character_maximum_length = 2;
Sign up to request clarification or add additional context in comments.

5 Comments

Problem is there are more than a dozen columns with this specific type in my schema, can postgres specifically point out the failing column?
...well if you can narrow down the problem from 100 to ~10 columns, it is an improvement. Next you have to just check your VALUES clause and see which string literal for those columns are wider than 2 characters.
You under-estimated my laziness :D But it is a solution thank you
I guess the bigger issue is that your table and insert have so many columns involved. Over the course of your life, most tables with which you deal probably won't have so many columns.
It is definitely an issue, and it's not the first problem caused by this design. Unfortunately as programmers we are not always the ones to design the databases we are using.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.