简体   繁体   English

Laravel 中的数据模型错误(SQLSTATE[42S01])

[英]Error with data model in Laravel (SQLSTATE[42S01])

Good morning, I'm trying to do the data models for a workshop workers app.早上好,我正在尝试为车间工人应用程序创建数据模型。 The problem comes with the guides and images data models.问题来自指南和图像数据模型。 Each guide has his own image, and every image belongs to a guide.每个向导都有自己的形象,每个形象都属于一个向导。 When I try to run the migrations and Laravel throws the SQLSTATE and I don't find the error, so I would appreciate your help.当我尝试运行迁移并且 Laravel 抛出 SQLSTATE 并且我没有发现错误时,所以我很感激你的帮助。

The guides migration:指南迁移:

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

The guides model:向导模型:

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;
    }


}

The Images migration:图片迁移:

        Schema::create('images', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('image');
            $table->timestamps();
        });

The Images model:图像模型:

class Image extends Model
{
    protected $fillable = ['image'];

    public function images()
    {
        return $this -> belongsTo(Guide::class);
    }
}

Update: php artisan:status image更新:php artisan:status 图像php artisan:status 截图

Order matter, since your 'guides' migration depends on 'images' primary key : 'images' migration must be executed before 'guides' .顺序很重要,因为您的'guides'迁移取决于'images'主键: 'images'迁移必须在'guides'之前执行。

You must change your migration files order.您必须更改迁移文件顺序。 see this link看到这个链接

Things you can try:你可以尝试的事情:

  • Make sure the images migration file comes before the guides migration file since the image_id is a foreign key on the guides table确保图像迁移文件位于指南迁移文件之前,因为 image_id 是指南表上的外键
  • Run php artisan migrate:reset运行php artisan migrate:reset

Move your image migration file upper to your guides migration file since in your guide migration file you are referencing the images_id in your foreign key.将您的图像迁移文件移至您的指南迁移文件上方,因为在您的指南迁移文件中,您正在引用外键中的 images_id。 To move it.要移动它。 just update the date of the images migration file.只需更新图像迁移文件的日期。

Since your guide migration was created on 2019_12_10 , your images table should be 2019_12_09 or lower .由于您的指南迁移是在2019_12_10创建的, 2019_12_10您的图像表应为2019_12_09 or lower Just rename it.只需重命名即可。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 laravel中如何修复“ SQLSTATE [42S01]:基本表或视图已存在”错误 - How to fix 'SQLSTATE[42S01]:base table or view already exists' error in laravel Laravel 错误:SQLSTATE[42S01]:基表或视图已存在:1050 表“类别”已存在 - Laravel Error : SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'categories' already exists 迁移php aritsan laravel 5.2 [PDOException] SQLSTATE [42S01] - Migrate php aritsan laravel 5.2 [PDOException] SQLSTATE[42S01] 错误处理请求:文件错误:-SQLSTATE [42S01]:基表或视图已存在: - Error Processing Request: Error in file: - SQLSTATE[42S01]: Base table or view already exists: 处理请求时出错:SQLSTATE [42S01]:基表或视图已存在:在magento 1.9中 - Error processing your request: SQLSTATE[42S01]: Base table or view already exists: in magento 1.9 警告:为“ catalog_items”创建夹具失败“ SQLSTATE [42S01] - Warning: Fixture creation for “catalog_items” failed " SQLSTATE[42S01] SQLSTATE [42S01]:基本表或视图已存在或基本表或视图已存在:1050表 - SQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Table SQLSTATE [42S01]:基本表或视图已存在:1050表'weee_tax'已存在,查询为 - SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'weee_tax' already exists, query was Illuminate\Database\QueryException SQLSTATE[42S01]:基表或视图已存在:1050 表“发票”已存在 - Illuminate\Database\QueryException SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'invoices' already exists SQLSTATE[42S01]:基表或视图已存在:1050 表“付款”已存在(SQL:创建表“付款” - SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'payments' already exists (SQL: create table `payments`
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM