我正在尝试将数据从 csv 文件导入 pg 表。我得到的错误是:
$ psql -U postgres -c "COPY users (first_name) FROM '/users.csv' (FORMAT csv)"
ERROR: attribute 51 of type users has wrong type
DETAIL: Table has type integer, but query expects character varying.
CONTEXT: COPY users, line 1
我怎么知道它是关于哪个属性的?就此而言, INSERT 也失败了:
$ psql -U postgres -c "INSERT INTO users (first_name) VALUES ('first_name')"
ERROR: attribute 51 of type record has wrong type
DETAIL: Table has type integer, but query expects character varying.
我试图根据\d users
输出和attnum
值进行计数:
SELECT a.*
FROM pg_attribute a
JOIN pg_class c on a.attrelid = c.oid
JOIN pg_namespace n on c.relnamespace = n.oid
WHERE n.nspname = 'public'
AND c.relname = 'users'
AND attnum >= 1
ORDER BY attnum
我正在运行 PostgreSQL 12.7。
数字对应于
pg_attribute.attnum
值。我按照以下建议对列重新排序。似乎索引是指使用attnum
值的列。换句话说,在重新排序列之后,他们开始引用其他列,这导致了错误。要重现问题(脚本),请创建一个表:
重新排序列:
尝试插入一行:
如您所见
t1_f2_idx
,现在指的是f3
(在表中它是一个整数,在索引中是一个字符串)。至少在这种情况下它是可逆的: