简体   繁体   English

Laravel 数据库迁移外键错误

[英]Laravel Database Migration foreign key error

I have two tables, transactions, and payments, which already exist from past migrations.我有两个表、交易和支付,它们在过去的迁移中已经存在。 When I try to make a pivot table between them, I get an error for the foreign key OF THE TRANSACTIONS ONLY.当我尝试在它们之间创建一个数据透视表时,我只收到一个关于事务的外键的错误。 The other foreign key, on the "payments" table, is created just fine. “支付”表上的另一个外键创建得很好。 This is absolutely baffling me, and none of the previous discussions I have found on this error have solved the problem.这绝对让我莫名其妙,我之前发现的关于这个错误的讨论都没有解决这个问题。

The Error (for reference, MAccounting is the name of the database):错误(供参考,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`))

*Despite what the error seems to say, the payment_transaction table is created successfully, along with the foreign key for the payments table; *尽管错误似乎说的是,payment_transaction 表已成功创建,以及支付表的外键; the only one that breaks is the foreign key for the transactions.唯一中断的是交易的外键。

The Transaction Table:交易表:

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');
        }

    }

The payments table:付款表:

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');
        }

    }

The Pivot table:数据透视表:

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');
        }

    }

******************* GOT IT WORKING, BUT I STILL NEED TO FIGURE OUT HOW THIS HAPPENED ******** Unfortunately, a clean install solved this problem. ******************* 让它工作了,但我仍然需要弄清楚这是怎么发生的 ******** 不幸的是,全新安装解决了这个问题。 That's fine and dandy, and I can keep developing now, but doing a clean install like that is not necessarily a convenience I have in a production environment.这很好,很花哨,我现在可以继续开发,但是像这样进行全新安装并不一定能让我在生产环境中获得便利。 I need to figure out what caused this/how to recreate it.我需要弄清楚是什么导致了这个/如何重新创建它。

you can fix all this mess by making payment_transaction.payment_id of type INT(10) unsigned .您可以通过使payment_transaction.payment_id类型为INT(10) unsigned来解决所有这些混乱。 in this case payments.id INT(10) AUTO_INCREAMENT will be equal and your references will work.在这种情况下, payments.id INT(10) AUTO_INCREAMENT将相等,您的参考将起作用。

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

For your pivot table use this migration:对于您的数据透视表,请使用此迁移:

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');
    });
}

Why don't you try this:你为什么不试试这个:

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

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

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