Unable To Update The Model

Hi Expert friends,

Im using the relational database with relations in my model.

when i’m trying to update my_Model(by uploading new image i.e, by deleting old image) i’m getting the following error. how can i overcome this problem.

CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (imember.organisation, CONSTRAINT organisation_logo FOREIGN KEY (logo_file_id) REFERENCES file (id) ON DELETE NO ACTION ON UPDATE NO ACTION). The SQL statement executed was: DELETE FROM file WHERE file.id=‘21’

my controller updateAction is like this:

public function actionUpdate($id)



		$fileModel = new File;


		$file = File::model()->findByPk($model->file_id);



		if (isset($_POST['MyModel'])) {

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

			if($fileModel->file = CUploadedFile::getInstance($model, 'file')){


				//deleting the old file,dir and record in file table if new file is uploading




				$fileModel->attributes = $_POST['MyModel'];




				if ($model->validate()) {



				if ($fileModel->saveImage()) {


					// update the organisation

					$model->file_id = $fileModel->id;

					if ($model->save())

how can i overcome this problem…

Any help pls

Thanks in advance

I am just going to paste some code which works for me:


		if ($file instanceof CUploadedFile) {


			if (!in_array(strtolower($file->getExtensionName()),array('gif','png','jpg','jpeg'))) {

				throw new CHttpException(500,CJSON::encode(

					array('error'=>'Invalid file extension '. $file->getExtensionName().'.')



			// $fileName=trim(md5($attribute.time().uniqid(rand(),true))).'.'.$file->getExtensionName();

			$fileName = $file->getName();


			if (file_exists($path) || !$file->saveAs($path)) {

				throw new CHttpException(500,CJSON::encode(

					array('error'=>'Could not save file or file exists: "'.$path.'".')



The error is very clear:

CONSTRAINT `organisation_logo` FOREIGN KEY (`logo_file_id`) REFERENCES `file` (`id`) 

You cannot delete the record in the table until you delete the corresponding record or NULL logo_file_id in the corresponding table.

Yes, I realised it. thanks for your reply. I’m trying to delete parent table record. i have deleted child record first then tried to delete parent record…its working fine now

thanks for your reply all :)