2014-09-04 17 views
0

我使用laravel架構構建和這裏的錯誤是我的代碼...MySQL的SQLSTATE [23000]在laravel

//create links table 
    Schema::create('links', function($table){ 
     $table->increments('id'); 
     $table->unsignedInteger('user_id'); 
     $table->unsignedInteger('domain_id'); 
     $table->string('url'); 
     $table->softDeletes(); 
     $table->timestamps(); 
    }); 
    Schema::table('links', function($table) { 
     $table->index('user_id'); 
     $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade'); 
     $table->index('domain_id'); 
     $table->foreign('domain_id')->references('id')->on('domains')->onDelete('cascade')->onUpdate('cascade'); 
    }); 
} 

但我嘗試的東西保存到該表它拋出下面的錯誤...

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`we`.`links`, CONSTRAINT `links_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE) (SQL: insert into `links` (`updated_at`, `created_at`) values (2014-09-04 17:35:53, 2014-09-04 17:35:53)) 

請建議如果您需要更多的東西來更好地理解這個,請幫我解決這個問題。

感謝

回答

1

如果SQL語句的某處被認爲應用程序的插入只是時間戳字段(的updated_at,created_at)。您至少需要爲查詢指定現有的user_id。如果有些情況下您可以在不關聯用戶的情況下插入links,則需要更新鏈接表,以便links.user_id允許爲空值。您無法通過模式管理器更新現有表上的列來接受空值,您需要通過手動查詢來完成。

+0

它實際上更新失敗。 – seoppc 2014-09-04 19:00:14

+0

我猜這個過程的一部分是生成註釋的SQL查詢,它包含在你的錯誤中:「insert into'links'('updated_at','created_at')values(2014-09-04 17:35: 53,2014-09-04 17:35:53)「。堆棧跟蹤在laravel.log中的樣子是什麼? – stakolee 2014-09-04 19:06:41

+0

'1452無法添加或更新子行:外鍵約束失敗('''''''鏈接',CONSTRAINT'links_user_id_foreign' FOREIGN KEY('user_id')REFERENCES'用戶'('ID')ON DELETE CASCADE ON UPDATE CASCADE)(SQL:插入'links'('updated_at','created_at')values(2014-09-04 19:12:57,2014-09-04 19:12:57))' – seoppc 2014-09-04 19:16:32

相關問題