CMD Dzeko
CMD Dzeko

Reputation: 1

Laravel 5.8 Migration foreign key problem?

I have 2 table. my code is working well on laravel 5.7 but when i using laravel. I always get error like this. anyone can help me?

Schema::create('tb_satuan', function (Blueprint $table) {
        $table->bigIncrements('id_satuan');
        $table->string('nama_satuan',40);
        $table->timestamps();
    });

    Schema::create('tb_user', function (Blueprint $table) {
        $table->bigIncrements('id_user');
        $table->BigInteger('id_satuan')->unsigned();
        $table->string('username',20);
        $table->string('email',30);
        $table->text('password');
        $table->timestamps();

        $table->foreign('id_satuan')->reference('id_satuan')->on('tb_satuan');
    });

This is the error:

Illuminate\Database\QueryException : SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ')' at line 1 (SQL: alter table tb_user add constraint tb_user_id_satuan_foreign foreign key (id_satuan) references tb_satuan ())

Upvotes: 0

Views: 229

Answers (2)

Md Azizur Rahman
Md Azizur Rahman

Reputation: 375

 public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('user_id');
            $table->string('title');
            $table->string('slug')->unique();
            $table->string('image')->default('default.png');
            $table->text('body');
            $table->integer('amount');
            $table->integer('room');
            $table->boolean('status')->default(false);
            $table->foreign('user_id')
                ->references('id')->on('users')
                ->onDelete('cascade');
            $table->timestamps();
        });
    }

Upvotes: 0

Mondini
Mondini

Reputation: 610

It's REFERENCES not REFERENCE

$table->foreign('id_satuan')->references('id_satuan')->on('tb_satuan');

Upvotes: 1

Related Questions