繁体   English   中英

Laravel 数据库迁移外键错误

[英]Laravel Database Migration foreign key error

我有两个表、交易和支付,它们在过去的迁移中已经存在。 当我尝试在它们之间创建一个数据透视表时,我只收到一个关于事务的外键的错误。 “支付”表上的另一个外键创建得很好。 这绝对让我莫名其妙,我之前发现的关于这个错误的讨论都没有解决这个问题。

错误(供参考,MAccounting 是数据库的名称):

[Illuminate\Database\QueryException]                                         
      SQLSTATE[HY000]: General error: 1005 Can't create table 'MRAccounting.#sql-  
      563_2d7' (errno: 150) (SQL: alter table `payment_transaction` add constrain  
      t payment_transaction_transaction_id_foreign foreign key (`transaction_id`)  
       references `transactions` (`id`))

*尽管错误似乎说的是,payment_transaction 表已成功创建,以及支付表的外键; 唯一中断的是交易的外键。

交易表:

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

    class CreateTransactionsTable extends Migration {

        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up()
        {
            Schema::create('transactions', function(Blueprint $table)
            {
                $table->increments('id');
                $table->string('name');
                $table->decimal('balance', 7,2);
                $table->integer('account_id');
                $table->integer('pending_id');
                $table->timestamps();
            });
        }


        /**
         * Reverse the migrations.
         *
         * @return void
         */
        public function down()
        {
            Schema::drop('transactions');
        }

    }

付款表:

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

    class CreatePaymentsTable extends Migration {

        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up()
        {
            Schema::create('payments', function(Blueprint $table)
            {
                $table->increments('id');
                $table->integer('account_to');
                $table->integer('account_from');
                $table->decimal('amount',7,2);
                $table->timestamps();
            });
        }


        /**
         * Reverse the migrations.
         *
         * @return void
         */
        public function down()
        {
            Schema::drop('payments');
        }

    }

数据透视表:

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

    class CreatePaymentTransactionTable extends Migration {

        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up()
        {
            Schema::create('payment_transaction', function(Blueprint $table)
            {
                $table->increments('id');
                $table->unsignedInteger('payment_id');
                $table->unsignedInteger('transaction_id');
                $table->timestamps();

                // $table->foreign('payment_id')->references('id')->on('payments');
                // $table->foreign('transaction_id')->references('id')->on('transactions');

            });

            Schema::table('payment_transaction', function(Blueprint $table){
                $table->foreign('payment_id')->references('id')->on('payments');
                $table->foreign('transaction_id')->references('id')->on('transactions');
            });


        }


        /**
         * Reverse the migrations.
         *
         * @return void
         */
        public function down()
        {
            Schema::drop('payment_transaction');
        }

    }

******************* 让它工作了,但我仍然需要弄清楚这是怎么发生的 ******** 不幸的是,全新安装解决了这个问题。 这很好,很花哨,我现在可以继续开发,但是像这样进行全新安装并不一定能让我在生产环境中获得便利。 我需要弄清楚是什么导致了这个/如何重新创建它。

您可以通过使payment_transaction.payment_id类型为INT(10) unsigned来解决所有这些混乱。 在这种情况下, payments.id INT(10) AUTO_INCREAMENT将相等,您的参考将起作用。

在主键和外键上使用标志 unsigned()。

对于您的数据透视表,请使用此迁移:

public function up() {
    Schema::create('payment_transaction', function(Blueprint $table) {
        $table->increments('id');
        $table->unsignedBigInteger('payment_id');
        $table->foreign('payment_id')->references('id')->on('payments');
        $table->unsignedBigInteger('transaction_id');
        $table->foreign('transaction_id')->references('id')->on('transactions');
    });
}

你为什么不试试这个:

$table->integer('payment_id')->unsigned();
$table->integer('transaction_id')->unsigned();

暂无
暂无

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

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