I am using laravel schema builder and here is my code...
//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');
});
}
But i try to save something to this table it throws following error...
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))
Please suggest if you need anything more to understand this better, help me to fix this.
thanks