【问题标题】:Can't create Primary Key-Foreign Key relation in Laravel无法在 Laravel 中创建主键-外键关系
【发布时间】:2020-04-04 11:37:11
【问题描述】:

我正在尝试在 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`))

在查看 SO 上的其他答案后,我似乎无法找出我哪里出错了,因为我已经对架构进行了更改。

当我运行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('departments', function (Blueprint $table) {
        $table->increments('id');
        $table->string('dept_name');
        $table->timestamps();

工资表

Schema::create('salaries', function (Blueprint $table) {
            $table->increments('id');
            $table->string('monthlySalary');
            $table->timestamps();

添加: 数据库播种机。

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 Seeder

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

【问题讨论】:

    标签: laravel-5 eloquent foreign-keys migration


    【解决方案1】:

    确保您在 employees 迁移中引用的表已经存在,因此可以实际引用它们。您可以通过重命名文件/更改 employees 迁移文件名中的日期部分来执行此操作,使其位于 departmentssalaries 迁移之后。

    你也需要改变

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

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

    以反映您在 salaries 迁移中使用的相同表名。

    https://laravel.com/docs/5.8/migrations#foreign-key-constraints

    【讨论】:

    • 感谢您指出薪水->薪水拼写错误。我已经更新了上面的问题以包含我当前使用的 DBSeeder 脚本。我改变了它,以便首先创建数据库和薪水,但仍然是同样的错误。
    • 播种机与这个问题和你得到的错误无关。 php artisan migrate 不会为数据库播种。如果您的迁移工作正常,并且您现在对播种机有不同的问题,请为您的播种机问题创建一个新问题。
    • 我认为播种机和迁移做同样的事情..显然我错了!谢谢!我已将您的答案标记为正确答案!
    • 不,迁移在您的数据库中创建/更改表/外键关系。播种者“seed”那些带有随机/虚假数据的表格,这样你就有了一些东西可以使用。您可以运行 php artisan db:seed 在运行迁移之后为数据播种。在您的 DEV 系统上,您可以运行 php artisan migrate:fresh --seed 以删除数据库中的所有数据并从一开始就运行所有迁移并运行所有播种机。不要在生产环境中运行它,因为它会删除您的所有数据
    猜你喜欢
    • 1970-01-01
    • 2017-08-29
    • 1970-01-01
    • 1970-01-01
    • 2020-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-18
    相关资源
    最近更新 更多