【问题标题】:laravel Foreign key constraint is incorrectly formedlaravel 外键约束格式不正确
【发布时间】:2017-10-03 16:51:43
【问题描述】:

嘿,伙计们,我尝试了太多东西并阅读了一些博客或讨论我没有解决我的问题我是 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');
}

我迁移表时每个表之间没有关系

【问题讨论】:

  • 您可以发布$table->foreign('address_id')->references('id')->on('addresses'); 引用的迁移吗?具有主键的表。
  • @BagusTesa 这是github repo链接github.com/emresandikci/moviestore-laravel
  • 那...很奇怪..我看不出有什么问题,我认为这是其中一个bigIncrementssmallIncrements..需要另一双眼睛来检查。
  • 等一下!刚刚意识到文件的 timestamp 顺序错误! 2017_05_04_115950_create_addresses_table.php2017_05_04_115721_create_orders_table.php 之前。您必须确保从属表(在本例中为 orders)位于它所引用的表(addresses)之后。很高兴您分享了您的项目,它使检查更容易。干杯!
  • address 表创建之后 orders 表而orders 表引用address 表外键,因此你得到一个错误.. 在Laravel,您必须使用时间戳订购表创建。因此,基本上,尝试更改迁移文件的时间戳部分 - 2017_05_04_115950_create_addresses_table.php。更简单的例子,也可以查看this repo,重新阅读docs also

标签: php mysql laravel foreign-keys constraints


【解决方案1】:

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

首先你运行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');
}

【讨论】:

  • 我做到了,所有的表都创建了,我没有收到任何错误,但是这次 ER 图上没有关系,你怎么看?我编辑了我的帖子,请检查 ss 的样子
猜你喜欢
  • 2019-07-25
  • 2017-10-04
  • 2018-02-19
  • 2021-05-26
  • 2017-04-13
  • 2019-09-17
  • 2020-09-24
  • 2019-11-02
  • 2023-03-19
相关资源
最近更新 更多