PostgreSQL - Herencia con claves?

 
Vista:

Herencia con claves?

Publicado por srgank (1 intervención) el 07/10/2010 09:19:21
Hola,

Estoy haciendo una base de datos con postgre y me apareció el siguiente problema:
Tengo una tabla con una clave primaria que tiene 2 hijos (por herencia) sin clave primaria. Al ponerle la sentencia inherits, resulta que si agrego un registro duplicado al padre se queja, pero si lo agrego a los hijos no se queja. Alguien tiene alguna solucion?

Aqui os dejo el codigo:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
REATE TEMPORARY TABLE Unidad (
    id SERIAL NOT NULL,
    id_producto INTEGER REFERENCES Producto(cod_producto) NOT NULL,
    fecha_compra DATE NOT NULL CHECK(fecha_compra <= CURRENT_DATE),
    UNIQUE(id, id_producto),
    PRIMARY KEY(id, id_producto)
);
 
CREATE TEMPORARY TABLE UnidadVenta (
    PrecioVenta INTEGER NOT NULL CHECK (PrecioVenta > 0)
 
)INHERITS(Unidad);
 
CREATE TEMPORARY TABLE UnidadAlquiler (
    Disponible BOOLEAN DEFAULT TRUE,
    PrecioDia INTEGER NOT NULL CHECK (PrecioDia > 0)
 
 
)INHERITS(Unidad) ;
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

Herencia con claves?

Publicado por DavidQs (1 intervención) el 31/12/2013 15:30:42
Estimado srgank.

A mi tambien se me presento el mismo inconveniente, encontre por google que postgres aun no realiza herencia de de PK ni Fk, lo que hice para solucionar el problema fue crear un trigger:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
CREATE OR REPLACE FUNCTION sp_tg_movimiento_pk (
)
RETURNS trigger AS
$body$
DECLARE
lcpkreg integer;
BEGIN
   select reg into lcpkreg from movimiento where (empresa=new.empresa and documento=new.documento);
   if lcpkreg notnull then
    	RAISE EXCEPTION 'LLave duplidada el registro se a grabado en otra tabla %';
   end if;
    RETURN null;
END;
$body$
LANGUAGE 'plpgsql'
VOLATILE
CALLED ON NULL INPUT
SECURITY INVOKER
COST 100;

Espero te sirva.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar