常规错误:1364字段“ manufacturer_name”没有默认值

时间:2019-05-07 17:49:17

标签: php mysql laravel foreign-keys database-migration

即使引用已在数据库中创建的制造商表中的ID列,但制造商名称也没有任何价值,并且与制造商名称位于同一行。为什么会这样呢?这是代码:

制造商表

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

飞机表

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

插入值:

制造商表:

$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')
            )); .....

飞机表:

同一循环只是不同的模式:

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

我希望平面表中的Manufacturer_name列内的增量ID指向Manufacturer表中的制造商名称。现在,所有行都只有默认值1。

以下是制造商数据库的外观:

enter image description here

我希望这些ID 2、3、4 ...作为制造商名称列中的外键位于Planes表中,但现在看起来像这样:

enter image description here

感谢帮助。

1 个答案:

答案 0 :(得分:0)

看看是否有帮助。

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

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

将数据插入表中时,您的代码必须插入正确的 外键值。迁移或数据库都无法为您做到这一点。