Gimana foreign key ada banyak dalam satu table

Schema::create('produks', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('user_id');
            $table->unsignedBigInteger('kategoriproduk_id');
            $table->string('slug');
            $table->string('nama_produk');
            $table->binary('gambar_produk1')->default('default.png');
            $table->text('deskripsi_produk');
            $table->bigInteger('harga');
            $table->integer('berat');
            $table->mediumInteger('stok');
            $table->string('lokasi_produk');
            $table->timestamps();
            $table->foreign('kategoriproduk_id')->references('id')->on('kategoriproduks');
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');

ERROR-NYA KAYAK GINI

Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1005 Can't create table `gudnut`.`produks` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `produks` add constraint `produks_kategoriproduk_id_foreign` foreign key (`kategoriproduk_id`) references `kategoriproduks` (`id`))

at C:\xampp\htdocs\GudNut\vendor\laravel\framework\src\Illuminate\Database\Connection.php:664 660| // If an exception occurs when attempting to run a query, we'll format the error 661| // message to include the bindings with SQL, which will make this exception a 662| // lot more helpful to the developer instead of just the database's errors. 663| catch (Exception $e) { > 664| throw new QueryException( 665| $query, $this->prepareBindings($bindings), $e 666| ); 667| } 668|

Exception trace:

1 PDOException::("SQLSTATE[HY000]: General error: 1005 Can't create table `gudnut`.`produks` (errno: 150 "Foreign key constraint is incorrectly formed")") C:\xampp\htdocs\GudNut\vendor\laravel\framework\src\Illuminate\Database\Connection.php:458

2 PDOStatement::execute() C:\xampp\htdocs\GudNut\vendor\laravel\framework\src\Illuminate\Database\Connection.php:458

Please use the argument -v to see more details.

avatar Juliand
@Juliand

40 Kontribusi 3 Poin

Diperbarui 4 tahun yang lalu

5 Jawaban:

import table kategoriproduks dulu

avatar driyan
@driyan

193 Kontribusi 109 Poin

Dipost 4 tahun yang lalu

udah, tetap ga bisa, apa harus dibuat table penyambung kayak many to many

avatar Juliand
@Juliand

40 Kontribusi 3 Poin

Dipost 4 tahun yang lalu

Tanggapan

kategoriproduks atau kategoriproduk ?

pakai integer biasa emang gabisa bro?

dibikin seperti ini saja

<pre> $table-&gt;integer('kategoriproduk_id')-&gt;unsigned()-&gt;nullable(); </pre>

avatar andynatalino
@andynatalino

23 Kontribusi 10 Poin

Dipost 4 tahun yang lalu

Coba edit dibagian ini <pre> $table-&gt;foreign('kategoriproduk_id')-&gt;references('id')-&gt;on('kategoriproduks'); </pre> Jadi <pre> $table-&gt;foreign('kategoriproduk_id')-&gt;references('id')-&gt;on('kategoriproduks')-&gt;onDelete('cascade'); </pre>

avatar bosque
@bosque

187 Kontribusi 104 Poin

Dipost 4 tahun yang lalu

Udah solved :) tapi yang lain masih error

avatar Juliand
@Juliand

40 Kontribusi 3 Poin

Dipost 4 tahun yang lalu

Login untuk ikut Jawaban