Skip to content

数据类型 Mismatch

错误信息

                列是 的类型 integer 但表达式是 的类型文本
              

快速摘要

值你 是 inserting or comparing 做不 match 列's 数据类型 — e.g., 文本到 integer 列.

为什么发生

  • Inserting 字符串到 integer or numeric 列

  • Comparing 列到 值的 不同类型 in WHERE clause

  • Application 发送错误类型 (e.g., 字符串相反的 数字)

  • Missing 类型 conversion (CAST or ::)

最小示例

✗ 错误代码
INSERT INTO users (id, name) VALUES ('abc', 'Ada');
-- id is INTEGER but 'abc' is text
✓ 修复代码
INSERT INTO users (id, name) VALUES (1, 'Ada');

id 列是 INTEGER, 所以它 需要数字, 不字符串. Using numeric 值 fixes 错误.

如何诊断

  • 读取错误到 see 哪个列 and 哪个类型是 expected 对比 provided

  • 检查列's 数据类型: \d table_name

  • 检查值 你的 application 是发送

如何修复

  • 发送正确类型从 你的 application

  • Use explicit 类型 conversion: CAST(值 AS 类型) or 值::类型

  • 更改列 类型如果 Schema 是错误

如何预防

  • Use parameterized 查询使用正确类型 — driver handles conversion

  • 验证数据类型 in 你的 application 之前 inserting

  • Use ORM 那个映射类型 automatically

相关资源

相关术语

相关课程

  • 表

    学习关于列 数据类型.

← 返回语言