Laravel 5 Can';t在Laravel中创建主键和外键关系

Laravel 5 Can';t在Laravel中创建主键和外键关系,laravel-5,eloquent,foreign-keys,migration,Laravel 5,Eloquent,Foreign Keys,Migration,我试图在laravel中创建一个主键-外键关系,但我一直遇到这个错误 SQLSTATE[HY000]: General error: 1005 Can't create table `volga_2`.`employees` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `employees` add constraint `employees_dept_id_foreign` fore

我试图在laravel中创建一个主键-外键关系,但我一直遇到这个错误

SQLSTATE[HY000]: General error: 1005 Can't create table `volga_2`.`employees` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `employees` add constraint `employees_dept_id_foreign` foreign key (`dept_id`) references `departments` (`id`))
我似乎无法找出我哪里出了问题,因为我已经在查看其他答案之后对模式进行了更改

当我运行
php artisan migrate
时,上面的错误会弹出,这是一个部分迁移

任何指向这一点的指针都将非常棒,因为我已经寻找了一段时间的解决方案

谢谢

员工表

Schema::create('employees', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->integer('dept_id')->unsigned();
            $table->foreign('dept_id')->references('id')->on('departments');
            $table->integer('salary_id')->unsigned();
            $table->foreign('salary_id')->references('id')->on('salary');
            $table->string('name');
            $table->string('gender');
            $table->timestamp('date_of_joining');
            $table->dateTime('date_of_birth');
            $table->timestamps();
Schema::create('salaries', function (Blueprint $table) {
            $table->increments('id');
            $table->string('monthlySalary');
            $table->timestamps();
部门表

Schema::create('departments', function (Blueprint $table) {
        $table->increments('id');
        $table->string('dept_name');
        $table->timestamps();
工资表

Schema::create('employees', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->integer('dept_id')->unsigned();
            $table->foreign('dept_id')->references('id')->on('departments');
            $table->integer('salary_id')->unsigned();
            $table->foreign('salary_id')->references('id')->on('salary');
            $table->string('name');
            $table->string('gender');
            $table->timestamp('date_of_joining');
            $table->dateTime('date_of_birth');
            $table->timestamps();
Schema::create('salaries', function (Blueprint $table) {
            $table->increments('id');
            $table->string('monthlySalary');
            $table->timestamps();
添加: DB播种机。

factory(App\Employee::class, 25)->create()->each(function ($employee) {
            $department = factory(App\Department::class)->make();
            $employee->department()->save($department);
            $salary = factory(App\Salary::class)->make();
            $employee->salary()->save($salary);
        });
根据评论更改了DB种子程序

factory(App\Department::class, 10)->create()->each(function ($department) {
            $salary = factory(App\Salary::class)->make();
            $department->salary()->save($salary);
            $employee = factory(App\Employee::class)->make();
            $salary->employee->save($employee);
        });

确保在
employees
迁移中引用的表已经存在,以便可以实际引用它们。您可以通过重命名文件/将
员工
迁移文件名中的日期部分更改为
部门
工资
迁移后的日期部分来完成此操作

你也需要改变

$table->foreign('salary_id')->references('id')->on('salary');

以反映您在
工资
迁移中使用的相同表名


感谢您指出工资->工资输入错误。我已经更新了上面的问题,以包括我当前使用的DBSeeder脚本。我对它进行了修改,以便首先创建数据库和薪资,但仍然是相同的错误。播种机与这个问题和您得到的错误无关
php artisan migrate
不会为数据库设置种子。如果您的迁移工作正常,并且您现在对播种机有不同的问题,请为您的播种机问题创建一个新问题。我认为播种机和迁移做了相同的事情…显然我错了!非常感谢。我已把你的答案标为正确答案!不,迁移会在数据库中创建/更改表/外键关系。播种机使用随机/虚假数据“播种”这些表,这样您就可以处理一些事情。您可以在运行迁移后运行
php-artisan-db:seed
为数据设定种子。在您的开发系统上,您可以运行
php artisan migrate:fresh--seed
删除数据库中的所有数据,从一开始就运行所有迁移,还可以运行所有种子程序。不要在生产环境中运行此操作,因为它将删除您的所有数据