簡體   English   中英

Laravel 中的數據模型錯誤(SQLSTATE[42S01])

[英]Error with data model in 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);
    }
}

更新:php artisan:status 圖像php artisan:status 截圖

順序很重要,因為您的'guides'遷移取決於'images'主鍵: 'images'遷移必須在'guides'之前執行。

您必須更改遷移文件順序。 看到這個鏈接

你可以嘗試的事情:

  • 確保圖像遷移文件位於指南遷移文件之前,因為 image_id 是指南表上的外鍵
  • 運行php artisan migrate:reset

將您的圖像遷移文件移至您的指南遷移文件上方,因為在您的指南遷移文件中,您正在引用外鍵中的 images_id。 要移動它。 只需更新圖像遷移文件的日期。

由於您的指南遷移是在2019_12_10創建的, 2019_12_10您的圖像表應為2019_12_09 or lower 只需重命名即可。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM