我正在尝试以编程方式将一个大型数据集从旧的MySQL数据库迁移到PostgreSQL。源数据一团糟,所以我的pgsql表有一堆约束来捕获进入的坏数据。
当insert由于这些约束之一而失败时,我的C程序如何发现哪个约束阻止了insert?
例如,其中一个较简单的表如下所示:
create table glue (
product_id int not null references product(id),
history_id int not null references history(id),
constraint glue_idx unique (product_id,history_id)
);
现在,我的程序最终执行了一个insert,它触发了其中一个约束,PQresultStatus只告诉我PGRES_FATAL_ERROR,而PQerrorMessage告诉我:
ERROR: duplicate key value violates unique constraint "glue_idx"
DETAIL: Key (product_id, history_id)=(413, 1762) already exists.
这对我这个人类来说是完全可读的。我的问题是,我的C程序如何识别glue_idx约束捕获的错误?
发布于 2018-08-03 07:24:01
使用函数
char *PQresultErrorField(const PGresult *res, int fieldcode)
它根据fieldcode
返回详细信息。可能的代码之一是PG_DIAG_CONSTRAINT_NAME
。
在the documentation.中查找函数描述
https://stackoverflow.com/questions/51663265
复制相似问题