laravel - 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();
}
}
解决方案
使用无符号列定义将外键绑定到 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');
});
}
推荐阅读
- java - 当我从屏幕返回时调用 OnChanged ()?
- c++ - 简单的互斥保护队列与 C++ 中的线程安全队列
- node.js - 如何使用 $lookup 而不是填充
- php - Laravel 迁移相同的外键依赖于 2 个不同的表
- kubernetes - 为什么 Kubernetes 返回未经授权的错误?
- react-native - Expo - 在物理设备上运行时独立 APK 崩溃;在 expo-cli 上运行的应用程序
- java - 通过java 8中的收集器max函数找出具有最大年龄的员工列表
- java - @PathVariable 与 @RequestBody(哪一个以及何时?)
- android-studio - 在 Project Navigator 的 Android Studio 中隐藏文件更新日期
- javascript - 无法在带有 DataTables 的 search.dt 搜索事件中使用 html() 以添加加载消息