php - 一般错误 : 1364 Field 'manufacturer_name' doesn't have a default value

标签 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。

以下是 Manufacturer DB 现在的样子:

enter image description here

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

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

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

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

关于php - 一般错误 : 1364 Field 'manufacturer_name' doesn't have a default value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56028014/

相关文章:

php - MySQL 查询太大?结果控制台无响应

php - 在 Laravel PHPUnit 中测试未经授权的用户限制

php - GWT 前端(托管模式)和 PHP 后端(apache)同时在本地主机上?

php - mysql php中每个项目的金额总和

mysql - 构建 MYSQL 查询所需的帮助

laravel - 在javascript代码中重定向惯性js和jetstream

laravel - Laravel 中的错误异常

php - 在开发过程中保证资源重新加载(通过ajax)的好策略是什么?

php - 如果php中的数据库查询没有返回结果,如何显示消息

php - 在 Laravel Eloquent 中扩展模型