百科 / 错误代码 / Class 42 语法错误或访问规则冲突
42804 datatype_mismatch
数据类型不匹配
ERROR 已实测 详解 实测通过
- 条件名
datatype_mismatch- 宏名称
ERRCODE_DATATYPE_MISMATCH- 启用版本
- 7.4
- 状态
- 活跃
版本覆盖
速览
42804 是 datatype_mismatch(数据类型不匹配):表达式或定义类型不同于目标上下文。本页选择整数列使用文本默认值。
含义
cookDefault 路径报告 column "%s" is of type %s but default expression is of type %s 并提示重写或 cast。同一 SQLSTATE 还覆盖 INSERT/UPDATE 目标赋值的 column "%s" is of type %s but expression is of type %s,以及 CASE/UNION 公共类型选择的 %s types %s and %s cannot be matched。它不同于 22P02(已选类型的输入解析器拒绝值)和 42846(选定公共目标没有转换路径)。检查 pg_attribute.atttypid、pg_type、pg_get_expr 以及实际表达式上下文;自动提交 CREATE 失败后为 IDLE。
诊断
确认目标列或结果类型,再检查 pg_attribute.atttypid、pg_type,以及默认值或生成表达式的 pg_get_expr。INSERT/UPDATE 要对照赋值表达式与目标列类型;CASE/UNION 要检查参与公共类型选择的每个分支或输入。先依据诊断中展开的源/目标类型和位置,再决定 cast。
处理
让表达式直接产生目标类型,或添加已理解语义的有效 cast。赋值场景应修正产生方或转成目标列类型;CASE/UNION 应让分支具有预期的公共类型。22P02 的输入错误不能靠改字符串后就当成类型匹配;如果没有转换路径,也应按源码区分 42846,不能误写成 42804。案例用 DEFAULT 1,插入默认行并读回 1。显式事务中定义或赋值失败会进入 INERROR,应先回滚或回到合适的 savepoint;本案例的自动提交失败才会回到 IDLE。
可复现案例
在一次性实例上执行过的场景。其中 1 个附有可执行 SQL,正文相应小节里给出。
default_type_mismatch PG 10 / 18 有 SQL
前置条件
- A runner-owned disposable target is provisioned.
触发
Define an integer column whose default expression is explicitly text.
断言
- SQLSTATE is 42804 with the type mismatch and rewrite/cast hint
- The failed autocommit session remains IDLE
- A matching integer default can be inserted and read back
处置
Make the expression type match the column or add a deliberate, valid cast; input-value errors such as 22P02 are a different path.
清理
Drop the runner schema and its table.
实测诊断
固定源码组都是 ERROR 变体。本案例 heap.c 主模板为 column "%s" is of type %s but default expression is of type %s,提示为 You will need to rewrite or cast the expression.;赋值路径使用 column "%s" is of type %s but expression is of type %s 和同一提示;CASE/UNION 等公共类型路径使用 %s types %s and %s cannot be matched。值输入失败是 22P02;选定公共类型后无法转换表达式则是 42846。
报文模板
源码里的格式串,不是某一次运行的输出。%s 之类是占位符,实际报文会填入对象名与取值。适用范围一栏是核验时留下的原始英文记录,未经翻译。
column "%s" is of type %s but default expression is of type %s
You will need to rewrite or cast the expression.
来源:src/backend/catalog/heap.c @ REL_18_6 · src/backend/catalog/heap.c @ REL_10_23
column "%s" is of type %s but expression is of type %s
You will need to rewrite or cast the expression.
来源:src/backend/parser/parse_target.c @ REL_18_6 · src/backend/parser/parse_target.c @ REL_10_23
%s types %s and %s cannot be matched
来源:src/backend/parser/parse_coerce.c @ REL_18_6 · src/backend/parser/parse_coerce.c @ REL_10_23
代表案例
注册表先定义整数列与显式文本默认值,再创建匹配定义、插入默认行并核对。
CREATE TABLE syntax_schema.mismatch_table (value integer DEFAULT 'x'::text);
CREATE TABLE syntax_schema.mismatch_table (value integer DEFAULT 1);
INSERT INTO syntax_schema.mismatch_table DEFAULT VALUES;
SELECT value FROM syntax_schema.mismatch_table;
选定的 18.6 与 10.21 运行均通过 SQLSTATE、严重级别、状态/恢复、修复、清理和一次性实例停止断言。详见 案例 JSON 与 作者证据;私有清单和注册表哈希也记录在其中。
版本
锁定目录从 7.4 存在边界起包含该条件并列出相关快照。选定自然案例已在 PostgreSQL 18.6 与 10.21 通过;这是有界观察,不能推断所有中间版本或所有源码分支。
来源
- 上游源码 src/backend/parser/parse_target.c 第 570–596 行
- 上游源码 src/backend/parser/parse_target.c 第 575–596 行
- 上游源码 src/backend/parser/parse_coerce.c 第 1349–1378 行
- 上游源码 src/backend/parser/parse_coerce.c 第 1564–1593 行
- 上游源码 src/backend/parser/parse_coerce.c 第 1217–1311 行
- 上游源码 src/backend/parser/parse_coerce.c 第 1327–1425 行
- 上游源码 src/backend/catalog/heap.c 第 2679–2686 行
- 上游源码 src/backend/catalog/heap.c 第 3413–3420 行
- 上游源码 src/backend/utils/errcodes.txt
- 核验材料 verify/cases/42804/cases.json
- 核验材料 verify/cases/42804/snippets.json
- 核验材料 raw/calls/REL_10_23.jsonl
- 核验材料 raw/calls/REL_18_6.jsonl
证据
断言
每条断言都写明了是怎么核实的,以及它不覆盖什么。这一层是核验时留下的原始英文记录,照原样呈现,未经翻译。
-
42804 is the datatype_mismatch condition in Class 42.
-
The selected default_type_mismatch follows a resolved PostgreSQL source-call group; other branches can use different dynamic fields.
-
Definition, target-column assignment, and CASE/UNION common-type contexts must agree on types; the fixed assignment and common-type branches are distinct from 22P02 input rejection and 42846 inability to coerce.
-
transformAssignedExpr raises 42804 when target-column coercion fails, while select_common_type raises 42804 when CASE/UNION inputs have incompatible type categories; a later failed coercion to an already selected common type is 42846.
-
The selected default_type_mismatch passed on isolated PostgreSQL 18.6 and 10.21 targets.
-
The locked catalogue records 42804 from the 7.4 presence bound through the listed snapshots; runtime scope is 18.6 and 10.21.
运行记录
| 目标 | 服务器版本 | 结果 | 覆盖案例 |
|---|---|---|---|
| latest | 18.6 (Homebrew) | passed | default_type_mismatch |
| pg10 | 10.21 (Debian 10.21-1.pgdg90+1) | passed | default_type_mismatch |
同类错误代码
Class 42 语法错误或访问规则冲突 下的其他成员。
42000syntax_error_or_access_rule_violation42501insufficient_privilege42601syntax_error42602invalid_name42611invalid_column_definition42622name_too_long42701duplicate_column42702ambiguous_column42703undefined_column42704undefined_object42710duplicate_object42712duplicate_alias42723duplicate_function42725ambiguous_function42803grouping_error42809wrong_object_type42830invalid_foreign_key42846cannot_coerce42883undefined_function428C9generated_always42939reserved_name42P01undefined_table42P02undefined_parameter42P03duplicate_cursor42P04duplicate_database42P05duplicate_prepared_statement42P06duplicate_schema42P07duplicate_table42P08ambiguous_parameter42P09ambiguous_alias42P10invalid_column_reference42P11invalid_cursor_definition42P12invalid_database_definition42P13invalid_function_definition42P14invalid_prepared_statement_definition42P15invalid_schema_definition42P16invalid_table_definition42P17invalid_object_definition42P18indeterminate_datatype42P19invalid_recursion42P20windowing_error42P21collation_mismatch42P22indeterminate_collation