在搜索之后,似乎问题是当插入表2时表1中不存在id但是它确实存在于我的表中!实际上直到昨天这个工作正常几个月。
错误:无法添加或更新子行:外键约束失败审核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();
}
}
答案 0 :(得分:0)
你的user_id是外键吗?如果是,可能它所指向的表没有一行,其中包含您尝试将其分配给
的用户ID