Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel 拉拉维尔:移民与就业;播种问题_Laravel_Laravel 6 - Fatal编程技术网

Laravel 拉拉维尔:移民与就业;播种问题

Laravel 拉拉维尔:移民与就业;播种问题,laravel,laravel-6,Laravel,Laravel 6,我正在尝试为数据库添加种子,但在运行php artisan migrate:fresh时出错: Illuminate\Database\QueryException SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `news` add constraint `news_author_id_foreign` foreign key (`author_id`) r

我正在尝试为数据库添加种子,但在运行
php artisan migrate:fresh
时出错:

 Illuminate\Database\QueryException

  SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `news` add constraint `news_author_id_foreign` foreign key (`author_id`) references `authors` (`id`) on delete cascade)

  at C:\laragon\www\startup-reporter\vendor\laravel\framework\src\Illuminate\Database\Connection.php:669
    665|         // If an exception occurs when attempting to run a query, we'll format the error
    666|         // message to include the bindings with SQL, which will make this exception a
    667|         // lot more helpful to the developer instead of just the database's errors.
    668|         catch (Exception $e) {
  > 669|             throw new QueryException(
    670|                 $query, $this->prepareBindings($bindings), $e
    671|             );
    672|         }
    673|

  1   C:\laragon\www\startup-reporter\vendor\laravel\framework\src\Illuminate\Database\Connection.php:463
      PDOException::("SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint")

  2   C:\laragon\www\startup-reporter\vendor\laravel\framework\src\Illuminate\Database\Connection.php:463
      PDOStatement::execute()
以下是迁移文件:

Schema::create('news', function (Blueprint $table) {
  $table->bigIncrements('id');
   ...
  $table->unsignedBigInteger('author_id');
  $table->foreign('author_id')
    ->references('id')
    ->on('authors')
    ->onDelete('cascade');
});
以下是工厂文件:

$factory->define(Author::class, function (Faker $faker) {
    return [
        'name' => $faker->name
    ];
});
最后,这里是种子文件:

// DatabaseSeeder
    {
        $this->call(AuthorsTableSeeder::class);
        $this->call(NewsTableSeeder::class);
    }

// NewsSeeder and AuthorSeeder

factory(App\News::class, 150)->create();
factory(App\Authors::class, 50)->create();
知道我为什么会出现这个错误吗?我能做些什么来修复它


谢谢。

因为
新闻的迁移文件在
作者之前运行


只需尝试更改这两个文件的时间戳,即可使
作者
迁移在
新闻
迁移

之前运行,因为
新闻
的迁移文件在
作者
之前运行


只要尝试更改这两个文件的时间戳,使
作者
迁移在
新闻
迁移

之前运行即可?我对拉雷维尔是半新手。另外,在我的DatabaseSeeder文件中,
作者
位于
新闻
(我将更新我的问题以说明这一点。我的意思是,当你创建一个新的迁移文件时,在migrations文件夹中会有这样一个时间戳:
2020\u 03\u 26\u 152343\u create\u news\u table.php
只要确保
authors
文件中的时间戳在news
文件之前就可以了,因为Laravel是按顺序运行迁移文件的。)r、 因此,当您运行
news
迁移文件时,它正在查找名为
author
的表,但找不到它,因此它引发了一个异常……这就是我该怎么做?我对Laravel是半新手。另外,在我的DatabaseSeeder文件中,
authors
位于
news
之前(我将更新我的问题以说明这一点。我的意思是,当你创建一个新的迁移文件时,在migrations文件夹中会有这样一个时间戳:
2020\u 03\u 26\u 152343\u create\u news\u table.php
只要确保
authors
文件中的时间戳在news
文件之前就可以了,因为Laravel是按顺序运行迁移文件的。)r、 因此,当您运行
新闻
迁移文件时,它正在查找一个名为
作者
的表,但找不到它,因此它抛出一个异常……就是这样
    return [
      ...
      'author_id' => $faker->numberBetween($min = 1, $max = 50),
    ];
// DatabaseSeeder
    {
        $this->call(AuthorsTableSeeder::class);
        $this->call(NewsTableSeeder::class);
    }

// NewsSeeder and AuthorSeeder

factory(App\News::class, 150)->create();
factory(App\Authors::class, 50)->create();