aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2012-05-15 12:12:40 -0400
committerTom Lane <tgl@sss.pgh.pa.us>2012-05-16 07:28:25 -0400
commit488c6dd1708a2c4a9e9f307001f28c05c48651bd (patch)
treed16baea4aecc29adca676952b26ab1c88d7b6c3d /src
parent6593c5b5dc39b179b1b7a3c947df2596af3e70c9 (diff)
downloadpostgresql-488c6dd1708a2c4a9e9f307001f28c05c48651bd.tar.gz
postgresql-488c6dd1708a2c4a9e9f307001f28c05c48651bd.zip
Improve error message for ALTER COLUMN TYPE coercion failure.
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.
Diffstat (limited to 'src')
-rw-r--r--src/backend/commands/tablecmds.c7
-rw-r--r--src/test/regress/expected/alter_table.out8
2 files changed, 9 insertions, 6 deletions
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index e23a3dab3cd..6148bd62da8 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -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
diff --git a/src/test/regress/expected/alter_table.out b/src/test/regress/expected/alter_table.out
index 456a729517f..a0ae5eb9a7b 100644
--- a/src/test/regress/expected/alter_table.out
+++ b/src/test/regress/expected/alter_table.out
@@ -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