2 FK to 1 PK

Asked

Viewed 161 times

2

I want to connect the 2 FK of the credit card table and bank bill to the Id_payment table plus the following error arises:

Error Code: 1022. Can’t write; Duplicate key in table '#sql-900_e'

Modelagem das Tabelas

I have no idea how it will solve, any help I appreciate

CREATE TABLE `cartao_credito` (
  `ID_Cartao` int(100) NOT NULL AUTO_INCREMENT,
  `Numero_Cartao` varchar(50) DEFAULT NULL,
  `Codigo_Seguranca` varchar(50) DEFAULT NULL,
  `Nome_Titular` varchar(50) DEFAULT NULL,
  `Validade_Mes` varchar(50) DEFAULT NULL,
  `Validade_Ano` varchar(50) DEFAULT NULL,
  `PagamentoID_Pagamento` int(100) NOT NULL,
  PRIMARY KEY (`ID_Cartao`),
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;


CREATE TABLE `boleto_bancario` (
  `ID_Boleto` int(100) NOT NULL AUTO_INCREMENT,
  `Cedente` varchar(50) DEFAULT NULL,
  `Agencia_Codigo` varchar(50) DEFAULT NULL,
  `Numero_Documento` varchar(50) DEFAULT NULL,
  `CPF_CNPJ` varchar(50) DEFAULT NULL,
  `Valor_Documento` varchar(50) DEFAULT NULL,
  `Codigo_Boleto` varchar(50) DEFAULT NULL,
  `Codigo_Banco` varchar(50) DEFAULT NULL,
  `Data_Vencimento` varchar(50) DEFAULT NULL,
  `Data_Documento` varchar(50) DEFAULT NULL,
  `N_Documento` varchar(50) DEFAULT NULL,
  `Nosso_Numero` varchar(50) DEFAULT NULL,
  `PagamentoID_Pagamento` int(100) NOT NULL,
  PRIMARY KEY (`ID_Boleto`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


CREATE TABLE `pagamento` (
  `ID_Pagamento` int(100) NOT NULL AUTO_INCREMENT,
  `Tipo_Pagamento` int(10) DEFAULT NULL,
  `PedidoID_Pedido` int(100) NOT NULL,
  `Agenda_ServicoID_Agenda` int(10) NOT NULL,
  PRIMARY KEY (`ID_Pagamento`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  • What exactly is the error command? You can add the command to create FK?

  • 15:29:47 ALTER TABLE prestadora.cartao_credito ADD CONSTRAINT PagamentoID_Pagamento FOREIGN KEY (PagamentoID_Pagamento) REFERENCES prestadora.pagamento (ID_Pagamento) ON DELETE NO ACTION ON UPDATE NO ACTION Error Code: 1022. Can’t write; Duplicate key in table '#sql-900_e' 0.141 sec

  • and I did the same in the boleto table and it worked

  • But did you put the exact same name on CONSTRAINT? This could be the problem.

  • and why it would be already that this referring to the same table ?

  • ALTER TABLE prestadora.boleto_bancario ADD CONSTRAINT PagamentoID_Pagamento FOREIGN KEY (PagamentoID_Pagamento) REFERENCES prestadora.pagamento (ID_Pagamento) ON DELETE NO ACTION ON UPDATE NO ACTION;

Show 1 more comment

1 answer

2


Try changing the name of the Constraint as follows:

ALTER TABLE prestadora.cartao_credito ADD CONSTRAINT FK_Pagamento 
FOREIGN KEY(PagamentoID_Pagamento) REFERENCES prestadora.pagamento (ID_Pagamento) 
ON DELETE NO ACTION ON UPDATE NO ACTION

In accordance with the documentation, Constraint identifier must be unique, otherwise an error similar to:

ERROR 1022 (2300): Can't write; duplicate key in table '#sql- 464_1'

Optionally the name can be omitted, being automatically generated by the database.

  • Thanks worked great so Fks can’t be the same ?

  • @Douglaslopes Yes, more details on https://dev.mysql.com/doc/refman/5.6/en/create-table-foreign-keys.html.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.