2012-07-17 64 views
0

我有一个外键问题,这是一个奇怪的问题。Postgres中的外键

第一个表:

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". 
+2

你有没有仔细检查600是否在'adjunto'?这两张表的内容是什么? – 2012-07-17 20:38:44

+2

我很确定Postgres没有说谎。您没有与该ID相关的行 – 2012-07-17 21:44:02

回答

1

我测试你的代码(我放弃了,因为被称为表adjunto_coleccion_privada_coleccion_fkey约束在粘贴代码不存在)。

我没有看到任何问题。

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