Improve error message for ALTER COLUMN TYPE coercion failure.
authorTom Lane <[email protected]>
Tue, 15 May 2012 16:12:40 +0000 (12:12 -0400)
committerTom Lane <[email protected]>
Wed, 16 May 2012 11:28:25 +0000 (07:28 -0400)
Per recent discussion, the error message for this was actually a trifle
inaccurate, since it said "cannot be cast" which might be incorrect.
Adjust that wording, and add a HINT suggesting that a USING clause might
be needed.

src/backend/commands/tablecmds.c
src/test/regress/expected/alter_table.out

index e23a3dab3cd0eceb9fe7e62373b71c0cfe607495..6148bd62da8d1261396c159963dcad8e6b6a81cb 100644 (file)
@@ -7315,8 +7315,9 @@ ATPrepAlterColumnType(List **wqueue,
        if (transform == NULL)
            ereport(ERROR,
                    (errcode(ERRCODE_DATATYPE_MISMATCH),
-                    errmsg("column \"%s\" cannot be cast to type %s",
-                           colName, format_type_be(targettype))));
+                    errmsg("column \"%s\" cannot be cast automatically to type %s",
+                           colName, format_type_be(targettype)),
+                    errhint("Specify a USING expression to perform the conversion.")));
 
        /* Fix collations after all else */
        assign_expr_collations(pstate, transform);
@@ -7482,7 +7483,7 @@ ATExecAlterColumnType(AlteredTableInfo *tab, Relation rel,
        if (defaultexpr == NULL)
            ereport(ERROR,
                    (errcode(ERRCODE_DATATYPE_MISMATCH),
-               errmsg("default for column \"%s\" cannot be cast to type %s",
+               errmsg("default for column \"%s\" cannot be cast automatically to type %s",
                       colName, format_type_be(targettype))));
    }
    else
index 456a729517f85ade34ea3fee4142ed101741ca3c..a0ae5eb9a7beb53fc7ef8f9eb2088a1427c77b9d 100644 (file)
@@ -1673,7 +1673,8 @@ select f3,max(f1) from foo group by f3;
 
 -- Simple tests for alter table column type
 alter table foo alter f1 TYPE integer; -- fails
-ERROR:  column "f1" cannot be cast to type integer
+ERROR:  column "f1" cannot be cast automatically to type integer
+HINT:  Specify a USING expression to perform the conversion.
 alter table foo alter f1 TYPE varchar(10);
 create table anothertab (atcol1 serial8, atcol2 boolean,
    constraint anothertab_chk check (atcol1 <= 3));
@@ -1688,7 +1689,8 @@ select * from anothertab;
 (2 rows)
 
 alter table anothertab alter column atcol1 type boolean; -- fails
-ERROR:  column "atcol1" cannot be cast to type boolean
+ERROR:  column "atcol1" cannot be cast automatically to type boolean
+HINT:  Specify a USING expression to perform the conversion.
 alter table anothertab alter column atcol1 type integer;
 select * from anothertab;
  atcol1 | atcol2 
@@ -1723,7 +1725,7 @@ select * from anothertab;
 
 alter table anothertab alter column atcol1 type boolean
         using case when atcol1 % 2 = 0 then true else false end; -- fails
-ERROR:  default for column "atcol1" cannot be cast to type boolean
+ERROR:  default for column "atcol1" cannot be cast automatically to type boolean
 alter table anothertab alter column atcol1 drop default;
 alter table anothertab alter column atcol1 type boolean
         using case when atcol1 % 2 = 0 then true else false end; -- fails