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.
MRAccounting
. Check the migration for that table, maybe? – Kirill Fuchs