【问题标题】:Syntax error or access violation: 1072 Key column 'kategori_id' doesn't exist in table语法错误或访问冲突:1072 表中不存在键列“kategori_id”
【发布时间】:2021-04-09 12:14:50
【问题描述】:

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

错误按摩

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

分类表

<?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');
    }
}

产品表

<?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()),
        ];
    }
}

【问题讨论】:

    标签: php laravel eloquent migration


    【解决方案1】:

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

    $table->unsignedBigInteger('foreing_id');
    

    然后,将其转换为外键:

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

    你错过了表 products 中的foreign字段:

    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();
            });
    

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

    【讨论】:

    • 天哪,非常感谢!我很高兴,我没有意识到从书中采取了错误的方法。
    • 我想补充一点,您可以将创建列的行和添加外键的行替换为同时执行这两个操作的一行:$table-&gt;foreign('kategori_id')-&gt;constrained(); 参见:link to laravel docs跨度>
    • @chilly 啊啊,是这样吗?好的,谢谢你的信息。 :)
    猜你喜欢
    • 2020-12-07
    • 2017-01-30
    • 1970-01-01
    • 1970-01-01
    • 2015-03-23
    • 1970-01-01
    • 2013-06-01
    • 2016-10-19
    • 2019-02-10
    相关资源
    最近更新 更多