Skip to content

Commit 1c150f8

Browse files
committedJun 12, 2015
Improve error message and hint for ALTER COLUMN TYPE can't-cast failure.
We already tried to improve this once, but the "improved" text was rather off-target if you had provided a USING clause. Also, it seems helpful to provide the exact text of a suggested USING clause, so users can just copy-and-paste it when needed. Per complaint from Keith Rarick and a suggestion from Merlin Moncure. Back-patch to 9.2 where the current wording was adopted.
1 parent e9eebba commit 1c150f8

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed
 

‎src/backend/commands/tablecmds.c

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7587,11 +7587,26 @@ ATPrepAlterColumnType(List **wqueue,
75877587
COERCE_IMPLICIT_CAST,
75887588
-1);
75897589
if (transform == NULL)
7590-
ereport(ERROR,
7591-
(errcode(ERRCODE_DATATYPE_MISMATCH),
7592-
errmsg("column \"%s\" cannot be cast automatically to type %s",
7593-
colName, format_type_be(targettype)),
7594-
errhint("Specify a USING expression to perform the conversion.")));
7590+
{
7591+
/* error text depends on whether USING was specified or not */
7592+
if (def->raw_default != NULL)
7593+
ereport(ERROR,
7594+
(errcode(ERRCODE_DATATYPE_MISMATCH),
7595+
errmsg("result of USING clause for column \"%s\""
7596+
" cannot be cast automatically to type %s",
7597+
colName, format_type_be(targettype)),
7598+
errhint("You might need to add an explicit cast.")));
7599+
else
7600+
ereport(ERROR,
7601+
(errcode(ERRCODE_DATATYPE_MISMATCH),
7602+
errmsg("column \"%s\" cannot be cast automatically to type %s",
7603+
colName, format_type_be(targettype)),
7604+
/* translator: USING is SQL, don't translate it */
7605+
errhint("You might need to specify \"USING %s::%s\".",
7606+
quote_identifier(colName),
7607+
format_type_with_typemod(targettype,
7608+
targettypmod))));
7609+
}
75957610

75967611
/* Fix collations after all else */
75977612
assign_expr_collations(pstate, transform);

‎src/test/regress/expected/alter_table.out

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1660,7 +1660,7 @@ select f3,max(f1) from foo group by f3;
16601660
-- Simple tests for alter table column type
16611661
alter table foo alter f1 TYPE integer; -- fails
16621662
ERROR: column "f1" cannot be cast automatically to type integer
1663-
HINT: Specify a USING expression to perform the conversion.
1663+
HINT: You might need to specify "USING f1::integer".
16641664
alter table foo alter f1 TYPE varchar(10);
16651665
create table anothertab (atcol1 serial8, atcol2 boolean,
16661666
constraint anothertab_chk check (atcol1 <= 3));
@@ -1675,7 +1675,10 @@ select * from anothertab;
16751675

16761676
alter table anothertab alter column atcol1 type boolean; -- fails
16771677
ERROR: column "atcol1" cannot be cast automatically to type boolean
1678-
HINT: Specify a USING expression to perform the conversion.
1678+
HINT: You might need to specify "USING atcol1::boolean".
1679+
alter table anothertab alter column atcol1 type boolean using atcol1::int; -- fails
1680+
ERROR: result of USING clause for column "atcol1" cannot be cast automatically to type boolean
1681+
HINT: You might need to add an explicit cast.
16791682
alter table anothertab alter column atcol1 type integer;
16801683
select * from anothertab;
16811684
atcol1 | atcol2

‎src/test/regress/sql/alter_table.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,6 +1174,7 @@ insert into anothertab (atcol1, atcol2) values (default, false);
11741174
select * from anothertab;
11751175

11761176
alter table anothertab alter column atcol1 type boolean; -- fails
1177+
alter table anothertab alter column atcol1 type boolean using atcol1::int; -- fails
11771178
alter table anothertab alter column atcol1 type integer;
11781179

11791180
select * from anothertab;

0 commit comments

Comments
 (0)