简体   繁体   中英

I can't insert null on a Foreign Key

I've tried to search but nothing works, and I don't know what to do. There's a table with two foreign keys, one of which can be null . According to what I've searched, it's perfectly fine to have null foreign keys. But no matter what, when I try to insert a null in that value, it fails. It says:

*Cause: A foreign key value has no matching primary key value.

*Action: Delete the foreign key or add a matching primary key.

Here is the code of the table. The FK that I want to be null is idPedido

CREATE TABLE PAGOS(
fechaLimite DATE,
cuantia NUMBER NOT NULL,
fechaInicio DATE DEFAULT SYSDATE,
fechaLiquidacion DATE,
idPago VARCHAR(8) NOT NULL,
dni VARCHAR(14) NOT NULL,
tipoPago VARCHAR(7) DEFAULT 'OTRO' CHECK(tipoPAGO IN('MENSUAL','PEDIDO','OTRO')),
idPedido VARCHAR2(10),
PRIMARY KEY(idPago),
FOREIGN KEY(dni) REFERENCES MIEMBROS ON DELETE SET NULL,
FOREIGN KEY(idPedido) REFERENCES PEDIDOS ON DELETE SET NULL
);

There are some triggers and such to add sequences for the idPago value. Here is the code of the procedure that creates a new item to the table:

create or replace PROCEDURE CREAR_PAGO(
  new_fechaLimite IN PAGOS.fechaLimite%TYPE ,
  new_cuantia IN PAGOS.cuantia%TYPE,
  new_fechaInicio IN PAGOS.fechaInicio%TYPE,
  new_fechaLiquidacion IN PAGOS.fechaLiquidacion%TYPE,
  new_dni IN PAGOS.dni%TYPE,
  new_tipoPago IN PAGOS.tipoPago%TYPE,
  new_idPedido IN PAGOS.idPedido%TYPE
)
  IS
BEGIN
  INSERT INTO PAGOS(fechaLimite,cuantia,fechaInicio,fechaLiquidacion,dni,tipoPago,idPedido) VALUES(new_fechaLimite,new_cuantia,new_fechaInicio,new_fechaLiquidacion,new_dni,new_tipoPago,new_idPedido);
END CREAR_PAGO;

And here is me trying to insert a new element:

execute CREAR_PAGO('01012020',40,'01012010',null,49035480D,null,null);

I've already tried to put both "NULL" and "DEFAULT NULL" in the table code after idPedido's type and nothing works

Please I need help

It looks like the primary key for your table is idPago, but I don't see it in your insert statement. If that is the case, it would appear that your issue is trying to add a record with no primary key...not that the foreign key is null.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM