laravel外键约束不正确

时间:2017-05-05 09:15:07

标签: php mysql laravel foreign-keys constraints

嘿伙计们我尝试了太多的东西并阅读了一些博客或讨论我没有解决我的问题我是laravel这个项目的新课程我的大学课...当我想创建数据库这个错误时我收到错误像

[Illuminate\Database\QueryException]                                         
SQLSTATE[HY000]: General error: 1005 Can't create table `moviestore`.`#sql-  
13df8_27` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL:  
alter table `orders` add constraint `orders_address_id_foreign` foreign ke  
y (`address_id`) references `addresses` (`id`)) 

这是我的代码

public function up()
{
    Schema::create('orders', function (Blueprint $table) {
        $table->engine = 'InnoDB';
        $table->increments('id');
        $table->dateTime('OrderDate');
        $table->integer('TotalCount');
        $table->decimal('TotalPrice',20,2);
        $table->integer('user_id')->unsigned();
        $table->integer('address_id')->unsigned();
        $table->timestamps();

        $table->foreign('user_id')->references('id')->on('users');
        $table->foreign('address_id')->references('id')->on('addresses');
    });

   // Schema::table('orders', function(Blueprint $table)
   // {
   //     $table->foreign('user_id')->references('id')->on('users');
   //     $table->foreign('address_id')->references('id')->on('addresses');
   // });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    DB::statement('SET FOREIGN_KEY_CHECKS=0');
    Schema::dropIfExists('orders');
    DB::statement('SET FOREIGN_KEY_CHECKS=1');
}

地址表代码

public function up()
{
    Schema::create('addresses', function (Blueprint $table) {
        $table->engine = 'InnoDB';
        $table->increments('id');
        $table->string('FullAddress');
        $table->string('AddressType');
        $table->string('PhoneNumber');
        $table->integer('user_id')->unsigned();
        $table->timestamps();

        $table->foreign('user_id')->references('id')->on('users');
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    DB::statement('SET FOREIGN_KEY_CHECKS=0');
    Schema::dropIfExists('addresses');
    DB::statement('SET FOREIGN_KEY_CHECKS=1');
}

迁移表时,每个表之间没有关系 enter image description here

1 个答案:

答案 0 :(得分:0)

我认为您需要更新迁移文件,如:

首先,您运行Addresses迁移,因为Addresses.idorders.address_id的外键

地址表代码

public function up()
{
    Schema::create('addresses', function (Blueprint $table) {
        $table->engine = 'InnoDB';
        $table->increments('id');
        $table->string('FullAddress');
        $table->string('AddressType');
        $table->string('PhoneNumber');
        $table->integer('user_id')->unsigned();
        $table->timestamps();

       $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    DB::statement('SET FOREIGN_KEY_CHECKS=0');
    Schema::dropIfExists('addresses');
    DB::statement('SET FOREIGN_KEY_CHECKS=1');
}

订单表

public function up()
{
    Schema::create('orders', function (Blueprint $table) {
        $table->engine = 'InnoDB';
        $table->increments('id');
        $table->dateTime('OrderDate');
        $table->integer('TotalCount');
        $table->decimal('TotalPrice',20,2);
        $table->integer('user_id')->unsigned();
        $table->integer('address_id')->unsigned();
        $table->timestamps();

       $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        $table->foreign('address_id')->references('id')->on('addresses')->onDelete('cascade');
    });


}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    DB::statement('SET FOREIGN_KEY_CHECKS=0');
    Schema::dropIfExists('orders');
    DB::statement('SET FOREIGN_KEY_CHECKS=1');
}