【问题标题】:Laravel create simple relationship for 2 table from single tableLaravel 从单个表中为 2 个表创建简单关系
【发布时间】:2016-03-13 11:00:42
【问题描述】:

我有 3 个模型:UserChangeMoneyCurrencyType,我与 UserChangeMoney 有简单的关系,我想添加与 ChangeMoneyCurrencyType 的表的另一个关系,但是我收到此错误:

  D:\xampp\htdocs\epay-pro>php artisan migrate

  [Illuminate\Database\QueryException]
  SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL
  : alter table `change_money` add constraint change_money_currency_id_foreig
  n foreign key (`currency_id`) references `currency_type` (`id`))

我目前的迁移是:

Schema::create('change_money',function(Blueprint $table){
    $table->increments('id');
    $table->tinyInteger('currency_type');
    $table->string('current_money');
    $table->integer('user_id')->unsigned();
    $table->foreign('user_id')->references('id')->on('users');
    $table->integer('currency');
    $table->timestamps();
});

我没有任何问题,现在我想在 CurrencyType 表中添加其他外键,例如:

Schema::create('change_money',function(Blueprint $table){
    $table->increments('id');
    $table->tinyInteger('currency_type');
    $table->string('current_money');
    $table->integer('user_id')->unsigned();
    $table->foreign('user_id')->references('id')->on('users');
    $table->integer('currency_id')->unsigned();
    $table->foreign('currency_id')->references('id')->on('currency_type');
    $table->timestamps();
});

currency_type 表迁移:

Schema::create('currency_type',function(Blueprint $table){
    $table->increments('id');
    $table->integer('user_id')->unsigned();
    $table->foreign('user_id')->references('id')->on('users');
    $table->string('currency_type','50');
    $table->char('currency_symbol','1');
    $table->timestamps();
});

【问题讨论】:

    标签: laravel laravel-5


    【解决方案1】:

    通过将外键拆分到其他迁移解决了问题

    class ForeignKeyBetweenChangeMoneyAndCurrencyType extends Migration
    {
        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up()
        {
            Schema::table('change_money', function (Blueprint $table) {
                $table->integer('currency_id')->unsigned();
                $table->foreign('currency_id')->references('id')->on('currency_type');
            });
        }
    
        /**
         * Reverse the migrations.
         *
         * @return void
         */
        public function down()
        {
            Schema::table('change_money', function(Blueprint $table) {
                $table->dropForeign(' change_money_currency_id_foreign');
            });
        }
    }
    

    【讨论】:

      【解决方案2】:

      Laravel 中的迁移按顺序运行。我现在没有您的迁移订单,但我认为您的 change_money 迁移比您的 currency_type 迁移首先运行。因此,当应用程序尝试将 ChangeMoney 表中的外键添加到 CurrencyType 表时,CurrencyType 表还不存在。

      尝试创建一个新的迁移,只添加 ChangeMoney 表和 CurrencyType 表之间的关系

      【讨论】:

      • 不要做任何改变,我得到错误:(我不知道是什么问题。帖子已更新。请查看
      【解决方案3】:

      您必须从两个表中删除数据(“change_money”、“currency_type”)。

      或者你可以关闭外键检查。在 MySQL 或 phpmyadmin 中运行:

      SET FOREIGN_KEY_CHECKS=0;
      

      在 Laravel 中运行迁移:php artisan migrate

      然后再次开启外键检查:

      SET FOREIGN_KEY_CHECKS=1;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-11-21
        • 1970-01-01
        • 1970-01-01
        • 2014-07-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多