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 | 9e86bc29b6e5dba11e687161c3520b90b570e491 (patch) | |
tree | ec6d19cb6004ce458dd885291635c98cfdf3e421 /src/backend/commands/tablecmds.c | |
parent | 553e576e05b50f9faffbd3dd721e44fc3746898d (diff) |
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 3f1ea0911ff..90aa786de6f 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -7321,11 +7321,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->raw_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); |