Question

Hello I am using sqlite3 for database management for my AM1808. I have created a new table named MilkRateChartEditDetail.

const char *SQL_CREATE_MILKRATECHARTDETAILEDITTABLE   = 
"CREATE TABLE IF NOT EXISTS MilkRateChartEditDetail ( \
    MilkRateChartEditDetailId   INTEGER, \
    MilkRateChartId         INTEGER \
        REFERENCES MilkRateChart(MilkRateChartId) \
        ON UPDATE RESTRICT ON DELETE RESTRICT, \
    EffectiveDate           DATE, \
    EffectiveShift          UNSIGNED TINYINT, \
    MilkType                UNSIGNED TINYINT, \
    RCD_RateChartId         UNSIGNED INT,\
    SocietyCode             STRING, \
    RateType                STRING, \
    FAT                     FLOAT, \
    LRCLR                   FLOAT, \
    SNF                     FLOAT, \
    Solid                   FLOAT, \
    Rate                    FLOAT, \
    NewRate                 FLOAT, \
    UpdatedBy               INTEGER \
        REFERENCES UserMaster(UsermasterId) \
        ON UPDATE RESTRICT ON DELETE RESTRICT, \
    UpdatedOn               DATE, \
    FlagGSM                 UNSIGNED TINYINT, \
    FlagUSB                 UNSIGNED TINYINT, \
    PRIMARY KEY (MilkRateChartEditDetailId))";

Now i am trying to insert data into the table using following query.

const char *INSERT_MILKRATECHARTEDITDETAILTABLE =
    "INSERT INTO MilkRateChartEditDetail \
         (MilkRateChartEditDetailId,MilkRateChartId, \
         EffectiveDate,EffectiveShift,MilkType, \
         RCD_RateChartId,SocietyCode,RateType, \
         FAT,LRCLR,SNF,Solid,Rate,NewRate, \
         UpdatedBy,UpdatedOn,FlagGSM,FlagUSB) \
     VALUES(NULL,?,?,?,?,?,?,?,?,?,?,?,?,?,NULL,?,?,?)";

My data is :

RateChartId : 1
EffectiveDate : 10-12-2012
Shift : E
RateType : FAT
MilkRateChartId : 30
FAT : 11.100000
SNF : 0.000000
Solid : 0.000000
CLR : 0.000000
Updatedby : 1
MilkType : C
Rate : 0.000000
NewRate : 0.000000

I am getting an Error:

INSERT MILKRATECHARTEDITDETAILTABLE, ENTRY :'constraint failed'

I can not understand what is the problem ? how can i get out of this ?

Was it helpful?

Solution

You have set few foreign key (FK for short) constraints, and also you have primary key constraint.

If you are trying to insert record with the same primary key (MilkRateChartEditDetailId), or using some values for FK (MilkRateChartId or UpdatedBy) that do not exist in foreign tables, you will have this error message.

Solution is to look more carefully whether these FK and PK are used properly, or drop them.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top