Data insertion error in Sqlserver + Delphi

Asked

Viewed 133 times

2

I have the following problem, when trying to register a new record in the database it presents me the following error

inserir a descrição da imagem aqui

below follows the codes:

open the form for registration.

procedure TFMHome.lbl_IncOSClick(Sender: TObject);
begin
UDM.ADODSOs.Open;
UDM.ADODSPecas_ordem_servico.Open;
UDM.ADODSOs.Insert;
UDM.ADODSOsData_Abertura.AsDateTime:= Date;
UDM.ADODSOs.Post;
UDM.ADODSOs.Edit;
FMOs.showmodal;
end;

Registration inclusion:

procedure TFMOs.Image1Click(Sender: TObject);
begin
UDM.ADODSOs.Post;
 Application.MessageBox(
'O registro foi incluido com sucesso.',
'Informação',MB_OK+MB_ICONINFORMATION);
//end;
end;

Database:

create table pecas (
ID_Peca int PRIMARY KEY IDENTITY (1,1) NOT NULL,
Nome varchar (50)NOT NULL,
Categoria varchar (20),
Unidade char (8),
Quantidade int NOT NULL,
Valor_Custo float NOT NULL,
Valor_Venda float NOT NULL,
Observacao varchar (max),
ID_Fornecedor INT NOT NULL FOREIGN KEY REFERENCES fornecedor (ID_Fornecedor)
)

create table veiculo (
ID_Veiculo int PRIMARY KEY IDENTITY (1,1) NOT NULL,
Placa varchar (15),
Modelo varchar (20),
Ano char (4),
Combustivel varchar (12),
Cor varchar (20),
N_Chassi varchar (30)NOT NULL,
Observacao varchar (max),
ID_CLIENTE INT NOT NULL FOREIGN KEY REFERENCES cliente (ID_CLIENTE)
)

create table ordem_servico(
ID_OrdemServico int PRIMARY KEY IDENTITY(1,1) NOT NULL,
Situacao varchar (20),
Km varchar (20),
Localizacao varchar (30),
Data_Abertura datetime,
Data_Fechamento datetime,
ID_Funcionario INT  FOREIGN KEY REFERENCES funcionario (ID_Funcionario),
ID_Cliente INT  FOREIGN KEY REFERENCES cliente (ID_Cliente),
ID_Peca INT  FOREIGN KEY REFERENCES pecas (ID_Peca),
ID_Veiculo INT FOREIGN KEY REFERENCES veiculo (ID_Veiculo)
)

create table pecas_ordem_servico(
ID_PECAS_ORDEMSERVICO int PRIMARY KEY IDENTITY (1,1) NOT NULL,
ID_PECA INT NOT NULL FOREIGN KEY REFERENCES PECAS (ID_peca),
ID_OrdemServico int NOT NULL FOREIGN KEY REFERENCES ordem_servico (ID_OrdemServico),
Valor_Unit float NOT NULL,
Qtde int NOT NULL,
Total float NOT NULL,
)

1 answer

0


The problem is foreign key, you are trying to include a record that depends on a foreign key, but the table of this key does not have the record.

By the key name of your message you can try to include a part in a service order that does not exist.

If you see that absolutely all keys are populated correctly in memory before including then check if it is including in the right order, it may be that it is trying to include a piece in the order before including the order.

  • but this is the order.Register Supplier > Register Part > Register Customer > Register Vehicle > Castrate Service Order containing Part, Customer, Vehicle

  • So either you register a part in a q order does not exist or at the time you register the Service Order containing Customer Order and Vehicle she is trying to place the relationship order request before the order. This kernel you have to check, the error message is clear, foreign key error, it has no different.

  • What recommends to be done?

  • Put in the question there all the Inserts he’s doing, then I can show you the order to be executed. Persistence layers usually have a log function, which one are you using? You know how to use its log?

Browser other questions tagged

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