【问题标题】:Foreign keys in PostgresPostgres 中的外键
【发布时间】:2012-07-17 20:24:30
【问题描述】:

我遇到了外键问题,这是一个奇怪的问题。

第一个表:

CREATE TABLE adjunto
(
  id serial NOT NULL,
  codigo text,
  descripcion text,
  usuario integer,
  file integer,
  nombre text,
  propiedades hstore,

  CONSTRAINT adjunto_pkey PRIMARY KEY (id ),
  CONSTRAINT adjunto_file_fkey FOREIGN KEY (file)
      REFERENCES file (file_id) MATCH SIMPLE 
      ON UPDATE NO ACTION ON DELETE CASCADE
) WITH (
  OIDS=FALSE
);

第二张桌子:

CREATE TABLE adjunto_coleccion_privada
(
  id serial NOT NULL,
  adjunto integer,
  coleccion integer,
  CONSTRAINT adjunto_coleccion_privada_pkey PRIMARY KEY (id ),
  CONSTRAINT adjunto_coleccion_privada_adjunto_fkey FOREIGN KEY (adjunto)
  REFERENCES adjunto (id) MATCH SIMPLE
  ON UPDATE NO ACTION ON DELETE CASCADE,
  CONSTRAINT adjunto_coleccion_privada_coleccion_fkey FOREIGN KEY (coleccion)
  REFERENCES coleccion (id) MATCH SIMPLE
  ON UPDATE NO ACTION ON DELETE CASCADE
)
WITH (
  OIDS=FALSE
);

命令:

INSERT INTO adjunto_coleccion_privada (adjunto, coleccion) 
VALUES (600, 2) RETURNING id

值 600 和 2 存在于 adjunto 和 colecion 两个表中。

详细错误:

Mensaje: ERROR: insert or update on table "adjunto_coleccion_privada" 
                violates foreign key   
                constraint "adjunto_coleccion_privada_adjunto_fkey"
Detail: Key (adjunto)=(600) is not present in table "adjunto".

【问题讨论】:

  • 您是否仔细检查了 600 是否在 adjunto 中?那两张表的内容是什么?
  • 我很确定 Postgres 没有说谎。您在 adjunto 中没有具有该 ID 的行

标签: postgresql foreign-keys


【解决方案1】:

我测试了您的代码(我删除了 adjunto_coleccion_privada_coleccion_fkey 约束,因为您粘贴的代码中不存在引用的表)。

我觉得一点问题都没有。

你确定adjunto表中有id = 600的记录吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-24
    • 1970-01-01
    • 2017-03-09
    • 2019-02-20
    • 2010-11-01
    • 1970-01-01
    相关资源
    最近更新 更多