Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit488c6dd

Browse files
committed
Improve error message for ALTER COLUMN TYPE coercion failure.
Per recent discussion, the error message for this was actually a trifleinaccurate, since it said "cannot be cast" which might be incorrect.Adjust that wording, and add a HINT suggesting that a USING clause mightbe needed.
1 parent6593c5b commit488c6dd

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

‎src/backend/commands/tablecmds.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7315,8 +7315,9 @@ ATPrepAlterColumnType(List **wqueue,
73157315
if (transform==NULL)
73167316
ereport(ERROR,
73177317
(errcode(ERRCODE_DATATYPE_MISMATCH),
7318-
errmsg("column \"%s\" cannot be cast to type %s",
7319-
colName,format_type_be(targettype))));
7318+
errmsg("column \"%s\" cannot be cast automatically to type %s",
7319+
colName,format_type_be(targettype)),
7320+
errhint("Specify a USING expression to perform the conversion.")));
73207321

73217322
/* Fix collations after all else */
73227323
assign_expr_collations(pstate,transform);
@@ -7482,7 +7483,7 @@ ATExecAlterColumnType(AlteredTableInfo *tab, Relation rel,
74827483
if (defaultexpr==NULL)
74837484
ereport(ERROR,
74847485
(errcode(ERRCODE_DATATYPE_MISMATCH),
7485-
errmsg("default for column \"%s\" cannot be cast to type %s",
7486+
errmsg("default for column \"%s\" cannot be castautomaticallyto type %s",
74867487
colName,format_type_be(targettype))));
74877488
}
74887489
else

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1673,7 +1673,8 @@ select f3,max(f1) from foo group by f3;
16731673

16741674
-- Simple tests for alter table column type
16751675
alter table foo alter f1 TYPE integer; -- fails
1676-
ERROR: column "f1" cannot be cast to type integer
1676+
ERROR: column "f1" cannot be cast automatically to type integer
1677+
HINT: Specify a USING expression to perform the conversion.
16771678
alter table foo alter f1 TYPE varchar(10);
16781679
create table anothertab (atcol1 serial8, atcol2 boolean,
16791680
constraint anothertab_chk check (atcol1 <= 3));
@@ -1688,7 +1689,8 @@ select * from anothertab;
16881689
(2 rows)
16891690

16901691
alter table anothertab alter column atcol1 type boolean; -- fails
1691-
ERROR: column "atcol1" cannot be cast to type boolean
1692+
ERROR: column "atcol1" cannot be cast automatically to type boolean
1693+
HINT: Specify a USING expression to perform the conversion.
16921694
alter table anothertab alter column atcol1 type integer;
16931695
select * from anothertab;
16941696
atcol1 | atcol2
@@ -1723,7 +1725,7 @@ select * from anothertab;
17231725

17241726
alter table anothertab alter column atcol1 type boolean
17251727
using case when atcol1 % 2 = 0 then true else false end; -- fails
1726-
ERROR: default for column "atcol1" cannot be cast to type boolean
1728+
ERROR: default for column "atcol1" cannot be castautomaticallyto type boolean
17271729
alter table anothertab alter column atcol1 drop default;
17281730
alter table anothertab alter column atcol1 type boolean
17291731
using case when atcol1 % 2 = 0 then true else false end; -- fails

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp