首页 > 解决方案 > SQLSTATE[HY000]:一般错误:1364 字段“album_id”没有默认值

问题描述

我想将多个图像链接到多个相册。我收到以下错误:

SQLSTATE[HY000]:一般错误:1364 字段“album_id”没有默认值

我不想为我的专辑 ID 设置默认值。

我的照片表:

public function up()
{
    Schema::create('fotos', function (Blueprint $table) {
        $table->increments('id');
        $table->string('foto');
        $table->timestamps();
        $table->integer('album_id');
    });
}

我的专辑表:

public function up()
{
    Schema::create('albums', function (Blueprint $table) {
        $table->increments('id');
        $table->string('foto');
        $table->string('naam');
        $table->timestamps();
    });
}

我的数据透视表:

public function up()
{
    Schema::create('album_foto', function (Blueprint $table) {
        $table->increments('id');
        $table->timestamps();
        $table->integer('album_id');
        $table->integer('foto_id');
    });
}

我的专辑表播种机:

<?php

use Illuminate\Database\Seeder;

use App\Album;

class AlbumsTableSeeder extends Seeder {

第一张专辑

public function run() {

  $album = new Album([

        'foto' => 'Vlinder.jpg',
        'naam' => 'Black & White'
    ]);
    $album->save();

第二张专辑

  $album = new Album([

        'foto' => 'Waterval2.jpg',
        'naam' => 'Mother Nature'
    ]);
    $album->save();
 }
}

我的照片表播种机:

<?php

use Illuminate\Database\Seeder;

use App\Foto;

class FotosTableSeeder extends Seeder {

图片第一张专辑

 public function run() {

  $foto = new Foto([
            'foto' => 'Vlinder.jpg'
        ],
        [
            'foto' => 'Berlijn.jpg'
        ],
        [
            'foto' => 'Mist.jpg'
        ],
        [
            'foto' => 'Mystery_Guy.JPG'
        ],
        [
            'foto' => 'Pop.JPG'
        ],
        [
            'foto' => 'Pop2.JPG'
        ],
        [
            'foto' => 'Pop3.JPG'
        ],
        [
            'foto' => 'Spiegel.JPG'
        ],
        [
            'foto' => 'Stammen.jpg'
        ],
        [
            'foto' => 'Voet.jpg'
        ],
        [
            'foto' => 'Vogels.jpg'
        ]
    );
    $foto->save();

图片第二张专辑

   $foto = new Foto([
            'foto' => 'Maan.jpg'
        ],
        [
            'foto' => 'Plant.JPG'
        ],
        [
            'foto' => 'Sneeuw.JPG'
        ],
        [
            'foto' => 'Stammen.jpg'
        ],
        [
            'foto' => 'Steen.JPG'
        ],
        [
            'foto' => 'Vlinder.jpg'
        ],
        [
            'foto' => 'Vogels.jpg'
        ]
    );
    $foto->save();
 }
}

标签: laravelseeding

解决方案


使用无符号列定义将外键绑定到 id。

还定义外键以正确连接您的表。

尝试以这种方式声明您的表:

<?php
// Fotos table 
public function up() {
    Schema::create('fotos', function (Blueprint $table) {
        $table->increments('id');
        $table->string('foto');
        $table->integer('album_id')->nullable()->unsigned();
        $table->timestamps();

        $table->foreign('album_id')->references('id')->on('albums');
    });
}

// Album table
public function up() {
    Schema::create('albums', function (Blueprint $table) {
        $table->increments('id');
        $table->string('foto');
        $table->string('naam');
        $table->timestamps();
    });
}

// Pivot table
public function up() {
    Schema::create('album_foto', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('album_id')->unsigned();
        $table->integer('foto_id')->unsigned();
        $table->timestamps();

        $table->foreign('album_id')->references('id')->on('albums');
        $table->foreign('foto_id')->references('id')->on('fotos');
    });
}

推荐阅读