我有两个表,交易和付款,这些表已在过去的迁移过程中存在。当我尝试在它们之间创建一个数据透视表时,我只得到外键的交易错误。另一个外键,在"付款"表,创建得很好。这绝对令我感到困惑,我之前在这个错误上找到的讨论都没有解决问题。
错误(供参考,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`))
*尽管错误似乎在说明,但pay_transaction表与pay表的外键一起成功创建;唯一中断的是交易的外键。
交易表:
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'); } }
******************* GOT工作,但我仍然需要弄清楚这是怎么发生的******** 不幸的是,干净的安装解决了这个问题。这很好,花花公子,现在我可以继续开发,但是像这样做一个干净的安装并不一定是我在生产环境中的便利。我需要弄清楚导致这个/如何重新创建它的原因。
答案 0 :(得分:1)
在主键和外键上使用标记unsigned()。
答案 1 :(得分:0)
你可以通过制作payment_transaction.payment_id
类型INT(10) unsigned
来解决所有这些问题。在这种情况下,payments.id
INT(10) AUTO_INCREAMENT
将是相同的,您的参考将起作用。
答案 2 :(得分:0)
你为什么不试试这个:
$table->integer('payment_id')->unsigned();
$table->integer('transaction_id')->unsigned();
答案 3 :(得分:0)
对于您的数据透视表,请使用此迁移:
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');
});
}