0
votes

I am getting bellow error recently my currently using app. when try to insert record using eloquent. This code was perfectly working in past few months. Now it just throwing the error after '1303' records inserted.

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicata du champ '1304' pour la clef 'PRIMARY' (SQL: insert into orderrequest

Translate from: English SQLSTATE [23000]: Integrity constraint violation: 1062 Duplicate field '1304' for the 'PRIMARY' key (SQL: insert into orderrequest

    $today = date("Y-m-d H:i:s");
    $order = OrderModel::find($id);
    $kot = new OrderrequestModel;
    $kot->orid = $id;
    $kot->location = $order->location;
    $kot->provider = "kitchen";

    $orderitems =OrderitemModel::where('orid', '=', $id)->get();
    $total = 0;


    foreach ($orderitems as $orderitem) {
        //$total = $total+($orderitem->price * $orderitem->qty);
        if($orderitem->provider=="kitchen"){
    $kot->token .= '<tr>
            <td align="left">'.$orderitem->fiid.'</td>
            <td align="left">'.$orderitem->item.'</td>
            <td align="center">'.$orderitem->provider.'</td>
            <td align="center">'.$orderitem->qty.'</td>

          </tr>';
          $orderitem->isreq = "k";
          $orderitem->save();
        }
    }
    $kot->token .= '</table>';
    $kot->type = "full";
    $kot->save();
2
you probably already have a record corresponding to same primary id in your table.Nitesh Verma
the curent PRIMARY key is 1303.Kombuwa
Check whether your primary_key is not auto incrimantalGayan
it is AUTO_INCREMENT and this code work perfectly over 1300 records.Kombuwa
id mediumint(9) AUTO_INCREMENTKombuwa

2 Answers

0
votes

Don’t know reason but after I have change the Type from mediumint(9) to int(11) now code is perfectly working. If you know the reason for this please comment bellow

0
votes

This error occurs when you are trying to insert into the database. Laravel writes that your primary key already exists in the database.

In the model, the primary key OrderitemModel orid? Did you write the data again.

Use OrderitemModel::findOrNew($orid)