diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2015-06-12 11:54:03 -0400 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2015-06-12 11:54:03 -0400 |
commit | b00982344a73d9cb626430dd17a6da84c15c9980 (patch) | |
tree | 91cb4eec1e3ab6f3c252934b453eaa02531022a0 /src/backend/commands/tablecmds.c | |
parent | b5fe62038f49f92c4a4f189c7cdacf3739effcdc (diff) | |
download | postgresql-b00982344a73d9cb626430dd17a6da84c15c9980.tar.gz postgresql-b00982344a73d9cb626430dd17a6da84c15c9980.zip |
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.
Diffstat (limited to 'src/backend/commands/tablecmds.c')
-rw-r--r-- | src/backend/commands/tablecmds.c | 25 |
1 files changed, 20 insertions, 5 deletions
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 84dbee0c411..d3947139c07 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -7888,11 +7888,26 @@ ATPrepAlterColumnType(List **wqueue, COERCE_IMPLICIT_CAST, -1); if (transform == NULL) - ereport(ERROR, - (errcode(ERRCODE_DATATYPE_MISMATCH), - errmsg("column \"%s\" cannot be cast automatically to type %s", - colName, format_type_be(targettype)), - errhint("Specify a USING expression to perform the conversion."))); + { + /* error text depends on whether USING was specified or not */ + if (def->cooked_default != NULL) + ereport(ERROR, + (errcode(ERRCODE_DATATYPE_MISMATCH), + errmsg("result of USING clause for column \"%s\"" + " cannot be cast automatically to type %s", + colName, format_type_be(targettype)), + errhint("You might need to add an explicit cast."))); + else + ereport(ERROR, + (errcode(ERRCODE_DATATYPE_MISMATCH), + errmsg("column \"%s\" cannot be cast automatically to type %s", + colName, format_type_be(targettype)), + /* translator: USING is SQL, don't translate it */ + errhint("You might need to specify \"USING %s::%s\".", + quote_identifier(colName), + format_type_with_typemod(targettype, + targettypmod)))); + } /* Fix collations after all else */ assign_expr_collations(pstate, transform); |