2014-01-28 86 views
0

我試圖創建一個用戶,但每次點擊創建,我得到這個錯誤:CDbCommand執行失敗SQLSTATE [23000]

CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint  violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`dcpcommunity`.`system_users`, CONSTRAINT `system_users_ibfk_1` FOREIGN KEY (`party_id`) REFERENCES `parties` (`id`)). The SQL statement executed was: INSERT INTO `system_users` (`status`, `date_modified`, `username`, `password`, `date_last_login`, `date_created`, `user_role`, `isLogin`) VALUES (:yp0, :yp1, :yp2, :yp3, :yp4, :yp5, :yp6, :yp7) 

,我讀了這個錯誤意味着我沒有'party_id'在我的表中,但我三重檢查和party_id作爲我的system_users表的主鍵存在。

我也看到它說REFERENCES parties,我檢查了我的模型的引用部分,並將其更改爲我的表名,我仍然得到這個錯誤。

public function relations() 
{ 
    // NOTE: you may need to adjust the relation name and the related 
    // class name for the relations automatically generated below. 
    return array(
     'party' => array(self::BELONGS_TO, 'system_users', 'party_id'), 
    ); 
} 

編輯

[system_users] 
pk: party_id 
username 
password 
date_last_login 
status 
date_created 
date_modified 
user_role 
isLogin 

沒有與域ID的締約方表和party_type_id

編輯

我檢查我的數據庫,並在我的system_users表,我發現該party_id與以下關係有關:

db.parties.id

如何在創建新記錄時使這種關係成爲可能?

+1

您能向我們展示您的派對和系統用戶架構的方案嗎? –

回答

1

作爲sql錯誤狀態:REFERENCES各方ID ......黨是在DB中的表。 AR將尋找稱爲Party的AR模型,如關係函數所述。 在AR模型中,「派對」應該是使用表格「派對」的定義。 錯誤來自數據庫(模式)。 創建操作嘗試插入不對應於system_users或參與方中的模式定義的值。

+0

有沒有辦法讓它連接到我希望它插入的表格?我通過Gii生成了這些模型,所以我不確定它爲什麼鏈接到錯誤的表格。 –

+0

你跟「連接桌子」是什麼? –

+0

我的意思是將模型從控制器傳遞到視圖(創建)。 –

相關問題