#Foreign Key constraint is incorrectly formed

9 messages · Page 1 of 1 (latest)

limber prism
#

Hey, need help with this. I'm trying to link the foreign key from "desas" table to the primary key of "kecamatans" table. But I encountered "foreign key is incorrectly formed" error. I don't know what's causing this. Is my foreign key correct?

"desas" table:

public function up()
    {
        Schema::create('desas', function (Blueprint $table) {
            $table->unsignedBigInteger('id', false);
            $table->primary('id');
            $table->string('nama_desa');
            $table->foreignId('kecamatan_id')->constrained()->onDelete('cascade');
            $table->timestamps();
        });
    }

"kecamatans" table:


public function up()
    {
        Schema::create('kecamatans', function (Blueprint $table) {
            $table->unsignedBigInteger('id', false);
            $table->primary('id');
            $table->string('nama_kecamatan');
            $table->foreignId('kabupaten_id')->constrained()->onDelete('cascade');
            $table->timestamps();
            
        });
    }

EDIT: Changed the foreign key type of "kecamatans" from "foreign" to "foreignId". Still have the same error

#

Have no idea what i'm doing wrong

worldly otter
#

post the full error

tranquil moon
#

@limber prism Why aren’t you following Laravel’s naming conventions?

limber prism
limber prism
tranquil moon
# limber prism

You can’t create foreign keys pointing to columns in tables that don’t exist yet.

limber prism
#

Okay so what i need to do is to create a migration for "kecamatans" table first then create "desas"?

#

like this? (previously desas table file is above kecamatans)