Schema::create('position', function (Blueprint $table) {
$table->increments('post_id');
$table->String('post_name');
$table->timestamps();
});
Schema::create('candidate', function (Blueprint $table) {
$table->increments('id');
$table->String('name');
$table->String('branch');
$table->unsignedInteger('post_id');
// $table->foreign('post_id_no')->references('post_id')->on('position')->onDelete('cascade');
$table->foreign('post_id')->references('post_id')->on('position')->onDelete('cascade');
$table->integer('count')->default(0);
$table->timestamps();
});
I have two tables, position
and candidate
. When I migrate I get error for foreign key. can anyone say whats error in code?
This is error I get when I migrate:
Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1005 Can't create table
voting
.#sql-16b7_2b
(errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter tablecandidate
add constraintcandidate_post_id_foreign
foreign key (post_id
) referencesposition
(post_id
) on delete cascade)
catch (Exception $e) {
throw new QueryException(
$query, $this->prepareBindings($bindings), $e
);
}
PDOException::("SQLSTATE[HY000]: General error: 1005 Can't create table `voting`.`#sql-16b7_2b` (errno: 150 "Foreign key constraint is incorrectly formed")")