mirror of
https://git.postgresql.org/git/postgresql.git
synced 2025-03-07 19:47:50 +08:00
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.
This commit is contained in:
parent
6593c5b5dc
commit
488c6dd170
@ -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
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user