General error: 1364 Field 'manufacturer_name' doesn't have a default value

General error: 1364 Field 'manufacturer_name' doesn't have a default value

我得到制造商名称没有值,即使它引用了制造商 table 中的 id 列,该列已经在数据库中创建并且与制造商名称位于同一行。为什么会这样?这是代码:

制造商table

Schema::create('manufacturer', function (Blueprint $table) {
    $table->bigIncrements('id')->autoIncrement();
    $table->string('manufacturer_name');
    $table->string('name');
    $table->timestamps();
});

飞机table

Schema::create('planes', function (Blueprint $table) {
        $table->bigIncrements('id')->autoIncrement();
        $table->string('icao24');
        $table->bigInteger('manufacturer_name')->unsigned()->default(1);
        $table->foreign('manufacturer_name')->references('id')->on('manufacturer');
        $table->timestamps();
    });

正在插入值:

制造商table:

$file = fopen('https://opensky-network.org/datasets/metadata/aircraftDatabase.csv', 'r');

        $headersArray = [];
        $i = 0;

        $headers = fgetcsv($file);

        foreach ($headers as $key => $value) {
            array_push($headersArray, $value);
        }

        while ($i < 50) {
            $line = fgetcsv($file);

            $comb = array_combine($headersArray, $line);

            DB::table('manufacturer')->insert(array(
                'name' => $comb['manufacturername'],
                'created_at' => date('Y-m-d H:m:s'),
                'updated_at' => date('Y-m-d H:m:s')
            )); .....

飞机table:

相同的循环只是不同的架构:

DB::table('planes')->insert(array(
                'icao24' => $comb['icao24'],
                'created_at' => date('Y-m-d H:m:s'),
                'updated_at' => date('Y-m-d H:m:s')
            ));

我希望在平面 table 中的 manufacturer_name 列中有增量 ID,指向 Manufacturer table 中的制造商名称。现在它只是所有行中的所有默认值 1。

以下是制造商数据库现在的样子:

enter image description here

我希望这些 id 2、3、4...在平面内 table 作为制造商名称列中的外键,但现在它看起来像这样:

enter image description here

感谢帮助。

看看这是否有帮助。

$file = fopen('https://opensky-network.org/datasets/metadata/aircraftDatabase.csv', 'r');

$headersArray = [];
$i = 0;

$headers = fgetcsv($file);

foreach ($headers as $key => $value) {
   array_push($headersArray, $value);
}

while ($i < 50) {
    $line = fgetcsv($file);

    $comb = array_combine($headersArray, $line);

    // Look up manuf to see if we already inserted it.
    // Hopefully name isn't duplicated
    $manuf = DB::table('manufacturer')->where('name', $comb['manufacturername'])->first();
    // Only insert if it isn't already there
    if ( ! $manuf ) {
        $manufId = DB::table('manufacturer')->insertGetId(array(
            'name' => $comb['manufacturername'],
        ));
    } else {
        $manufId = $manuf->id;
    }

    DB::table('planes')->insert(array(
        'icao24' => $comb['icao24'],
        'manufacturer_name' => $manufId
    ));

}

迁移和外键

Schema::create('planes', function (Blueprint $table) {
    $table->bigIncrements('id')->autoIncrement();
    $table->string('icao24');
    $table->bigInteger('manufacturer_name')->unsigned()->default(1); // (1)
    $table->foreign('manufacturer_name')->references('id')->on('manufacturer'); // (2)
    $table->timestamps();
});

迁移仅描述如何在数据库中构建 tables。它们不会将数据插入到 table 中。通过在上面定义 (1) 和 (2) 正在定义一列并描述该列与另一列的关系 table.

将数据插入 table 时,您的代码必须插入正确的 外键值。迁移或数据库不会为您执行此操作。