我正在使用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))
请建议您是否需要更多信息来更好地理解这一点,请帮助我解决此问题。
感谢
答案 0 :(得分:1)
如果某个地方的SQL语句被认为应用程序只插入了时间戳字段(updated_at,created_at)。您至少需要为查询指定现有的user_id。如果在某些情况下您可以在不关联用户的情况下插入links
,则需要更新链接表,以便links.user_id允许空值。您无法通过架构管理器更新现有表上的列以接受空值,您需要通过手动查询来执行此操作。