2011-09-27 67 views
0

我在schema.yml中定義這個實體學說,symfony的1.4:我如何解決這個「完整性約束違規」

Jobsearch: 
    tableName: jobsearch 
    columns: 
    seeker_id: 
     primary: true 
     type: integer 
     notnull: true 
     autoincrement: false 
    empmode: 
     type: string(50) 
    pensum: 
     type: integer 
    active: 
     type: boolean 
    relations: 
    Privateaccount: 
     local: seeker_id 
     foreign: id 
     alias: seeker 

它有一個外鍵引用

Privateaccount: 
    tableName: privateaccount 
    columns: 
    id: 
     primary: true 
     unique: true 
     type: integer 
     notnull: true 
     autoincrement: true 
    firstname: 
     type: string(255) 
    lastname: 
     type: string(255) 
    inheritance: 
    extends: sfGuardUser 
    type: column_aggregation 
    keyField: type 
    keyValue: Privateaccount 

我做用於測試目的的symfony動作,它應該將Jobsearch保存到db:

public function executeTest(sfWebRequest $request) 
    { 
    $test = new Jobsearch(); 
    $test->setSeeker($this->getUser()->getGuardUser()) ; // set the user that is logged in  
    $test->save(); 
    } 

$test->save()結果在這個錯誤:

SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (test2 . sf_guard_user_profile , CONSTRAINT sf_guard_user_profile_user_id_sf_guard_user_id FOREIGN KEY (user_id) REFERENCES sf_guard_user (id) ON DELETE CASCADE)

我不明白爲什麼外鍵約束失敗。

是什麼導致了錯誤?

編輯:如果我更改primaryfalseseeker_id,它確實有效。但我希望外鍵成爲主鍵。如果可能的話,我該如何做這項工作?

回答

0

嘗試改變的schema.yml到

Jobsearch: 
    tableName: jobsearch 
    columns: 
    seeker_id: 
     primary: true 
     type: integer 
     notnull: true 
     autoincrement: false 
    empmode: 
     type: string(50) 
    pensum: 
     type: integer 
    active: 
     type: boolean 
    relations: 
    Seeker: 
     class: Privateaccount 
     local: seeker_id 

Privateaccount: 
    tableName: privateaccount 
    columns: 
    firstname: 
     type: string(255) 
    lastname: 
     type: string(255) 
    inheritance: 
    extends: sfGuardUser 
    type: column_aggregation 
    keyField: type 
    keyValue: Privateaccount 

學說默認創建的id字段。不要聲明它。

最後,它應該工作。

public function executeTest(sfWebRequest $request) 
    { 
    $test = new Jobsearch(); 
    $test->setSeekerId($this->getUser()->getGuardUser()->getId()) ; // set the user that is logged in  
    $test->save(); 
    } 
相關問題