I am trying to add FK 'module_id' to my table 'documents'. I have ran the following query:
public function up()
{
Schema::table('documents', function (Blueprint $table) {
$table->integer('module_id')->unsigned();
$table->foreign('module_id')->references('id')->on('modules');
});
}
The following error is being returned:
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table
documentsadd constraintdocuments_module_id_foreignforeign key (module_id) referencesmodules(id))
I'm not sure what I'm doing wrong, I'm sure it is probably a silly mistake but I have spent a lot of time going around in circles trying to figure it out... here is what I have tried..
- both tables are already created
- the data types for both column's are consistent (both unsignedBigInts, 20)
I have included a picture of my DB tables, i appreciate any help.

Update:
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (
laravel.#sql-2cd_23, CONSTRAINTdocuments_module_id_foreignFOREIGN KEY (module_id) REFERENCESmodules(id)) (SQL: alter tabledocumentsadd constraintdocuments_module_id_foreignforeign key (module_id) referencesmodules(id))