语法错误或访问冲突:1072 table 中不存在键列 'kategori_id'

Syntax error or access violation: 1072 Key column 'kategori_id' doesn't exist in table

有人知道这个问题吗? 我正在使用 Laravel 8,我真的不知道是哪个问题。 我已经坚持这个多月了。

错误消息

\SQLSTATE[42000]:语法错误或访问冲突:1072 table 中不存在键列 'kategori_id'(SQL:更改 table produks 添加约束 produks_kategori_id_foreign 外键 (kategori_id) 引用 kategoris (id))

类别Table

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateKategorisTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('kategoris', function (Blueprint $table) {
            $table->id();
            $table->string('nama');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('kategoris');
    }
}

产品Table

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateProduksTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('produks', function (Blueprint $table) {
            $table->id();
            $table->char('kode',6)->unique();
            $table->string('nama');
            $table->foreign('kategori_id')->references('id')->on('kategoris');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('produks');
    }
}

类别工厂

<?php

namespace Database\Factories;

use App\Models\Kategori;
use Illuminate\Database\Eloquent\Factories\Factory;

class KategoriFactory extends Factory
{
    /**
     * The name of the factory's corresponding model.
     *
     * @var string
     */
    protected $model = Kategori::class;

    /**
     * Define the model's default state.
     *
     * @return array
     */
    public function definition()
    {
        $daftar_kategori = ["ABB","3M", "Autonics", "Supreme", "Omron"];

        return [
        'id' => $this->faker->numberBetween(1, \App\Models\Kategori::count()),
        'nama' => $this->faker->unique()->randomElement($daftar_kategori),
        ];
    }
}

产品工厂

<?php

namespace Database\Factories;

use App\Models\Produk;
use Illuminate\Database\Eloquent\Factories\Factory;

class ProdukFactory extends Factory
{
    /**
     * The name of the factory's corresponding model.
     *
     * @var string
     */
    protected $model = Produk::class;

    /**
     * Define the model's default state.
     *
     * @return array
     */
    public function definition()
    {
        $daftar_produk=["Inverter", "Rellay", "Lampu", "Push Button"];

        return [
            'kode'        => strtoupper($this->faker->unique()->bothify('??###')),
            'nama'        => $this->faker->randomElement($daftar_produk),
            'kategori_id' => $this->faker->numberBetween(1,
                            \App\Models\Kategori::count()),
        ];
    }
}

在Schema::create中添加外键时,需要先添加相应的字段,例如:

$table->unsignedBigInteger('foreing_id');

然后,将其转换为外键:

$table->foreign('foreign_id')->references('id')->on('foreign_table');

您错过了 table 产品中的外国字段:

Schema::create('produks', function (Blueprint $table) {
            $table->id();
            $table->char('kode',6)->unique();
            $table->string('nama');
add this--> $table->unsignedBigInteger('kategori_id');
            $table->foreign('kategori_id')->references('id')->on('kategoris');
            $table->timestamps();
        });

运行 再次迁移,问题应该会消失。