简体   繁体   English

Laravel 5.4迁移错误

[英]Laravel 5.4 migrate error

I made some migrations and when I tried to migrate I am getting an error. 我进行了一些迁移,当我尝试迁移时遇到错误。 I don't know where I made the mistake, I am new to Laravel migrations. 我不知道我在哪里犯了错误,我是Laravel迁移的新手。

Below is the code of my migration file 以下是我的迁移文件的代码

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateFavouritesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('favourites', function (Blueprint $table)
        {
            $table->increments('id');
            $table->integer('user_id')->unsigned();
            $table->integer('name_id')->unsigned();
            $table->timestamps();
        });
        Schema::table('favourites', function(Blueprint $table)
        {
            $table->foreign('user_id')->references('id')->on('users')->onDelete('CASCADE');
            $table->foreign('name_id')->references('id')->on('names')->onDelete('CASCADE');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('favourites');
        Schema::table('favourites', function(Blueprint $table)
        {
            $table->dropForeign(['user_id']);
            $table->dropForeign(['name_id']);
        });
    }
}

` and when I migrate, this is the error that I am getting: 在迁移时,这是我得到的错误:

 [Illuminate\Database\QueryException]
  SQLSTATE[HY000]: General error: 1005 Can't create table `ugal`.`#sql-187c_52` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `favour
  ites` add constraint `favourites_name_id_foreign` foreign key (`name_id`) references `names` (`id`) on delete CASCADE)



  [PDOException]
  SQLSTATE[HY000]: General error: 1005 Can't create table `ugal`.`#sql-187c_52` (errno: 150 "Foreign key constraint is incorrectly formed")

Where is the mistake,and why? 错误在哪里,为什么?

您应该在favourites表之前迁移表names

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM