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

时间:2018-12-17 16:21:09

标签: laravel seeding

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

  

SQLSTATE [HY000]:常规错误:1364字段'album_id'没有默认值

我不想为我的album_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();
 }
}

我的fotos表播种机:

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

1 个答案:

答案 0 :(得分:0)

使用无符号列定义将外键绑定到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');
    });
}