Domanda

After searching around, it seems like the problem is that the id doesn't exist in table 1 when inserting into table 2 BUT it does exist in my table! Actually this was working fine for few months until yesterday.

Error: Cannot add or update a child row: a foreign key constraint fails review CONSTRAINT reviews_ibfk_1 FOREIGN KEY (user_id) REFERENCES user(user_id))

public function actionAjaxComment($id) {
  $prod_id= $id;    

  if(Yii::app()->request->isAjaxRequest) {
    $model=new Review;
    $this->performAjaxValidation($model);

    $user_id = Yii::app()->user->user_id;

    $criteria = new CDbCriteria;
    $criteria->compare('target_product_id',$prod_id);
    $criteria->compare('user_id',$user_id);
    $record = $model->findAll($criteria);

    $model->attributes=$_POST['Review'];

    if(empty($record)) {
      $date = Yii::app()->getDateFormatter()->formatDateTime(time(), 'long', 'short');

      $model->user_id = $user_id;
      $model->target_product_id = $prod_id;
      $model->review_date = time();
      $uname = ucwords(Yii::app()->user->username);
      $img = User::model()->FindByPk($user_id)->image;
      $img_url = Yii::app()->basePath."/images/user/".$img;
      $img = Yii::app()->assetManager->publish($img_url);
      $model->save();

      echo CJSON::encode(
        array('status'=>'success',
              'star'=>$model->star,
              'review'=>$model->review,
              'user'=>$uname,
              'uimg'=>$img,
              'date'=>$date,
      ));

      return $model->review;
    } elseif(!empty($record)) {
      echo CJSON::encode(
        array('status'=>'dup'
      ));
    } else
      echo CJSON::encode(
        array('status'=>'error'
      ));

    Yii::app()->end();
  }
}

Nessuna soluzione corretta

Altri suggerimenti

Is your user_id a foreign key? If yes, the maybe the table which it points to doesn't have a row with the user id you are trying to assign it to

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top