选择 打开 改范围 完整检索页

pgsql.cc 提供对 postgresql.org 官网内容的中文翻译,由 Pigsty 团队维护。

百科 / 错误代码 / Class 42 语法错误或访问规则冲突

42804 datatype_mismatch

数据类型不匹配

ERROR 已实测 详解 实测通过

条件名
datatype_mismatch
宏名称
ERRCODE_DATATYPE_MISMATCH
启用版本
7.4
状态
活跃

版本覆盖

速览

42804datatype_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.atttypidpg_typepg_get_expr 以及实际表达式上下文;自动提交 CREATE 失败后为 IDLE

诊断

确认目标列或结果类型,再检查 pg_attribute.atttypidpg_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
HINT 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
HINT 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 通过;这是有界观察,不能推断所有中间版本或所有源码分支。

来源

证据

断言

每条断言都写明了是怎么核实的,以及它不覆盖什么。这一层是核验时留下的原始英文记录,照原样呈现,未经翻译。

运行记录

目标服务器版本结果覆盖案例
latest 18.6 (Homebrew) passed default_type_mismatch
pg10 10.21 (Debian 10.21-1.pgdg90+1) passed default_type_mismatch

同类错误代码

Class 42 语法错误或访问规则冲突 下的其他成员。