dsm0688 2015-01-05 00:25
浏览 20

too long

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):


    [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; 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.

  • 写回答

3条回答 默认 最新

  • dongqixuan3112 2015-01-05 23:50
    关注

    you can fix all this mess by making payment_transaction.payment_id of type INT(10) unsigned. in this case payments.id INT(10) AUTO_INCREAMENT will be equal and your references will work.

    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算