小编Mag*_*nus的帖子

PostgreSQL UNIQUE 约束错误消息

附录 A:PostgreSQL 错误代码中,它说:

对于某些类型的错误,服务器会报告与错误关联的数据库对象(表、表列、数据类型或约束)的名称;例如,导致 unique_violation 错误的唯一约束的名称。此类名称在错误报告消息的单独字段中提供,以便应用程序无需尝试从消息的可能本地化的人类可读文本中提取它们。

大胆强调我的。我有下表:

CREATE TABLE recipes (
    id SERIAL,
    title TEXT UNIQUE NOT NULL,
    description TEXT NOT NULL,
    instructions TEXT NOT NULL,
    image BYTEA,
    CONSTRAINT recipes_pk PRIMARY KEY(id),
    CONSTRAINT title_unique UNIQUE(title)
);
Run Code Online (Sandbox Code Playgroud)

当我尝试插入具有重复标题的新行时,我在 pgAdmin3 中收到以下错误消息:

ERROR:  duplicate key value violates unique constraint "title_unique"
DETAIL:  Key (title)=(mytitle) already exists.
Run Code Online (Sandbox Code Playgroud)

或者,使用 PHP:

["errorInfo"]=>
  array(3) {
    [0]=>
    string(5) "23505"
    [1]=>
    int(7)
    [2]=>
    string(117) "ERROR:  duplicate key value violates unique constraint "title_unique"
                 DETAIL:  Key (title)=(mytitle) already exists." …
Run Code Online (Sandbox Code Playgroud)

postgresql php error-handling unique-constraint

5
推荐指数
1
解决办法
1万
查看次数