Postingan lainnya
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.
5 Jawaban:
pakai integer biasa emang gabisa bro?
dibikin seperti ini saja
<pre> $table->integer('kategoriproduk_id')->unsigned()->nullable(); </pre>
Coba edit dibagian ini <pre> $table->foreign('kategoriproduk_id')->references('id')->on('kategoriproduks'); </pre> Jadi <pre> $table->foreign('kategoriproduk_id')->references('id')->on('kategoriproduks')->onDelete('cascade'); </pre>