From: | Marcelo Fernández <fernandezm22(at)yahoo(dot)com(dot)ar> |
---|---|
To: | Reynier Perez Mira <rperezm(at)uci(dot)cu> |
Cc: | pgsql-es-ayuda(at)postgresql(dot)org |
Subject: | Re: [pgsql-es-ayuda] RE: [pgsql-es-ayuda] Problemas con llaves foráneas |
Date: | 2008-10-13 19:35:31 |
Message-ID: | 48F3A303.9000802@yahoo.com.ar |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-es-ayuda |
Reynier Perez Mira escribió:
> Hola Marcelo:
> ¿En la tabla modlic_licencias es donde debería poder dejarse el valor "id_categoria" como NULL cierto? Bien le he seteado el valor a NULL y nada me sigue dando el mismo error. Esta es la consulta SQL que se ejecuta cuando creo la FK.
>
> ALTER TABLE public.modlic_categorias
> ADD CONSTRAINT fk_catlic
> FOREIGN KEY (id_categoria)
> REFERENCES public.modlic_licencias(id_categoria)
> ON DELETE SET NULL
> ON UPDATE CASCADE;
>
Ah, bueno, ahora sí puedo ver el problema. :-)
Primero, el ALTER TABLE debería ser sobre modlic_licencias (creo que
esto es obvio), no sobre modlic_categorias. Y segundo, es que el campo
modlic_licencias.id_categoria, al no ser PK, no tiene un índice creado
que lo haga único [1]. Esto lo lográs diciendo que id_categoria es UNIQUE.
Pasé tu ejemplo a una base de prueba:
test=# CREATE TABLE public.modlic_licencias (
test(# id_licencia bigint NOT NULL,
test(# id_categoria bigint NULL UNIQUE,
test(# id_autor bigint NOT NULL,
test(# nombre varchar(50),
test(# nodvd integer NOT NULL,
test(# fecha_creacion date,
test(# version varchar(10),
test(# documentacion text,
test(# sitio varchar(150),
test(# fecha_vencimiento date,
test(# privativo boolean NOT NULL DEFAULT true,
test(# CONSTRAINT licencia_software_pkey
test(# PRIMARY KEY (id_licencia)
test(# ) WITH (
test(# OIDS = FALSE
test(# );
NOTICE: CREATE TABLE / PRIMARY KEY creará el índice implícito
«licencia_software_pkey» para la tabla «modlic_licencias»
NOTICE: CREATE TABLE / UNIQUE creará el índice implícito
«modlic_licencias_id_categoria_key» para la tabla «modlic_licencias»
CREATE TABLE
test=#
test=# CREATE TABLE public.modlic_categorias (
test(# id_categoria integer NOT NULL,
test(# titulo_categoria varchar(150) NOT NULL,
test(# activa boolean NOT NULL DEFAULT false,
test(# CONSTRAINT modlic_categorias_pkey
test(# PRIMARY KEY (id_categoria)
test(# ) WITH (
test(# OIDS = FALSE
test(# );
NOTICE: CREATE TABLE / PRIMARY KEY creará el índice implícito
«modlic_categorias_pkey» para la tabla «modlic_categorias»
CREATE TABLE
test=# ALTER TABLE public.modlic_licencias
test-# ADD CONSTRAINT fk_catlic
test-# FOREIGN KEY (id_categoria)
test-# REFERENCES public.modlic_licencias(id_categoria)
test-# ON DELETE SET NULL
test-# ON UPDATE CASCADE;
ALTER TABLE
test=#
Ahora inserto valores en categorias:
test=# INSERT INTO modlic_categorias VALUES(1,'Categoria 1', True);
INSERT 0 1
test=# INSERT INTO modlic_categorias VALUES(2,'Categoria 2', True);
INSERT 0 1
test=# select * from modlic_categorias ;
id_categoria | titulo_categoria | activa
--------------+------------------+--------
1 | Categoria 1 | t
2 | Categoria 2 | t
(2 filas)
Ahora inserto valores en licencias (la Licencia 2 va a ser la que tenga
categoria NULL):
test=# INSERT INTO modlic_licencias VALUES(100,1,1000,'Licencia
1',0,NULL,NULL,NULL,NULL,NULL,True);
INSERT 0 1
test=# INSERT INTO modlic_licencias VALUES(150,NULL,1001,'Licencia
2',0,NULL,NULL,NULL,NULL,NULL,True);
INSERT 0 1
test=# INSERT INTO modlic_licencias VALUES(170,2,1004,'Licencia
3',0,NULL,NULL,NULL,NULL,NULL,False);
INSERT 0 1
Ahora revisamos que la 'Licencia 2' tenga una categoría NULL:
test=# select id_licencia, id_categoria, id_autor, nombre from
modlic_licencias ;
id_licencia | id_categoria | id_autor | nombre
-------------+--------------+----------+------------
100 | 1 | 1000 | Licencia 1
150 | | 1001 | Licencia 2
170 | 2 | 1004 | Licencia 3
(3 filas)
Espero que ésto sea lo que andabas buscando. :-)
Saludos,
Marcelo
--
Marcelo F. Fernández
Buenos Aires, Argentina
Licenciado en Sistemas - CCNA
E-Mail: fernandezm22(at)yahoo(dot)com(dot)ar
Jabber ID: fernandezm22(at)jabber(dot)org
Public Key ID: 5C990A6C 111C3661
Blog: http://marcelosoft.blogspot.com
From | Date | Subject | |
---|---|---|---|
Next Message | Reynier Perez Mira | 2008-10-13 19:44:51 | RE: [pgsql-es-ayuda] Actualización de PostgreSQL me acabó con la BD |
Previous Message | Reynier Perez Mira | 2008-10-13 19:21:11 | RE: [pgsql-es-ayuda] Problemas con llaves foráneas |