简体   繁体   中英

Foreign Key conflict on INSERT statement

I'm developing a WinForm desktop application for users to input employees retirement data, using SQL Server 2008 as DB.

One of the tables that gets part of the user data has a reference to another table whose records were defined at design time, adding a Foreign Key constraint for consistency.

CREATE TABLE tbCongedo (
    ID int IDENTITY(0,1) PRIMARY KEY,
    ID_ANAGRAFICA int NOT NULL,
    ID_TIPOLOGIA int NOT NULL,
    DECORRENZA datetime NOT NULL,
    PROT_NUM nvarchar(7) NOT NULL,
    PROT_DATA datetime NOT NULL
    );

CREATE TABLE tbTipologia (
    ID int IDENTITY(0,1) PRIMARY KEY,
    TIPOLOGIA nvarchar(20)
    );

INSERT INTO tbTipologia VALUES ('CONGEDO'), ('POSTICIPO'), ('ANTICIPO'), ('REVOCA'), ('DECESSO')

ALTER TABLE tbCongedo
ADD CONSTRAINT FK_tbCongedo_tbTipologia
FOREIGN KEY (ID_TIPOLOGIA) REFERENCES tbTipologia(ID)

Then, I have this code that should execute the INSERT statement

public int Insert(SqlConnection Connessione)
{
    using (SqlCommand Comando = new SqlCommand("INSERT INTO tbCongedo VALUES (@ID_ANAGRAFICA, @PROT_NUM, @PROT_DATA, @DECORRENZA, @ID_TIPOLOGIA); SELECT SCOPE_IDENTITY()", Connessione))
    {
        Comando.Parameters.AddWithValue("@ID_ANAGRAFICA", ID_ANAGRAFICA);
        Comando.Parameters.AddWithValue("@PROT_NUM", PROT_NUM);
        Comando.Parameters.AddWithValue("@PROT_DATA", PROT_DATA);
        Comando.Parameters.AddWithValue("@DECORRENZA", DECORRENZA);
        Comando.Parameters.AddWithValue("@ID_TIPOLOGIA", ID_TIPOLOGIA);

        ID = Int32.Parse(Comando.ExecuteScalar().ToString());
    }
    return ID;
}

but I'm given this SqlException:

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_tbCongedo_tbTipologia". The conflict occurred in database "Scadenziario_ver4_TEST", table "dbo.tbTipologia", column 'ID'

These are the data that I was trying to get inserted in the table:

ID_ANAGRAFICA = 2
ID_TIPOLOGIA = 0
PROT_DATA = {16/03/2018 00:00:00}
DECORRENZA = {16/03/2018 00:00:00}
PROT_NUM = 123456

Funny thing is that when I try to insert those same data manually through SQL Server Management Studio, I'm given no error at all.

Thanks.-

Try specifying fields: (col_name1, col_name2, ...) .

Without that the VALUES may not be applied as how you might hope. Variable names are NOT automagically matched with similarly-named columns.

So like this:

... new SqlCommand
(
    "INSERT INTO tbCongedo " +
    "       (ID_ANAGRAFICA, PROT_NUM, PROT_DATA, DECORRENZA, ID_TIPOLOGIA) "
    "VALUES (@ID_ANAGRAFICA, @PROT_NUM, @PROT_DATA, @DECORRENZA, @ID_TIPOLOGIA); " + 
    "SELECT SCOPE_IDENTITY()", Connessione
)
...

I think the problem isn't in the data but in the INSERT statement itself. You are trying to insert the values to the wrong columns using the wrong order. To solve the issue you should either specify the columns in the INSERT statement or correct the order of the values. In your case the query will try to insert the value of @PROT_NUM in the ID_TIPOLOGIA instead.

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