php - Laravel 中的数据模型错误 (SQLSTATE[42S01])
问题描述
早上好,我正在尝试为车间工人应用程序做数据模型。问题来自指南和图像数据模型。每个向导都有自己的形象,每个形象都属于一个向导。当我尝试运行迁移并且 Laravel 抛出 SQLSTATE 并且我没有找到错误时,非常感谢您的帮助。
指南迁移:
Schema::create('guides', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('title');
$table->text('topic')->nullable();
$table->string('author')->nullable();
$table->year('year')->nullable();
$table->text('goal')->nullable();
$table->text('description')->nullable();
$table->smallInteger('assistants_quantity')->nullable();
$table->smallInteger('duration_hours')->nullable();
$table->string('methodology')->nullable();
$table->bigInteger('image_id')->unsigned();
$table->foreign('image_id')->references('id')->on('images');
$table->timestamps();
$table->softDeletes();
});
导轨型号:
class Guide extends Model
{
protected $table = 'guides';
public $timestamps = true;
use SoftDeletes;
protected $dates = ['deleted_at'];
protected $fillable = ['title', 'topic', 'author', 'year', 'goal', 'description', 'assistants_quantity', 'duration_hours', 'methodology'];
protected $visible = ['title','topic', 'author', 'year', 'goal', 'description', 'assistants_quantity', 'duration_hours', 'methodology'];
public function materials()
{
return $this -> belongsToMany(Material::class);
}
public function images()
{
return $this -> belongsTo(Image::class);
}
static function search($keyword)
{
$result = Guide::where('title', 'LIKE', '%' . $keyword . '%')->get();
return $result;
}
}
图片迁移:
Schema::create('images', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('image');
$table->timestamps();
});
图像模型:
class Image extends Model
{
protected $fillable = ['image'];
public function images()
{
return $this -> belongsTo(Guide::class);
}
}
解决方案
你可以尝试的事情:
- 确保图像迁移文件位于指南迁移文件之前,因为 image_id 是指南表上的外键
- 跑
php artisan migrate:reset
推荐阅读
- sql-server - 将文本转换为多列的 SQL Server 大小写表达式
- angular - 如何从 OkObjectResult 获取价值并从 Angular6 订阅?
- python - 在天猫找不到价格的AJAX
- ssh - 主机密钥格式
- design-patterns - 应用服务能否在 DDD 的有界上下文中调用其他应用服务?
- c# - 动态元素命令绑定/INotifyPropertyChanged - TKCustomMap
- azure-devops - VSTS 分支策略可以应用于 GitHub 吗?
- couchdb - 我可以使用没有 url 的数据库名称在 CouchDB 中开始复制吗?
- android - 如何在房间数据库中使用多项选择
- sumo - SUMO 中的结果命令