この悪名高いエラーが発生しており、何が問題になったのか分かりません。MySQL 1215外部キー制約を追加できません - Laravel 5
class CreateOrdersTable extends Migration
{
public function up()
{
Schema::create('orders', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->integer('customer_id')->unsigned();
$table->integer('partner_id')->unsigned();
$table->string('status', 20)->default(Order::getDefaultStatus());
$table->string('paid', 20)->default('no');
$table->decimal('visitation_charges', 20, 2)->default(0);
$table->decimal('taxes', 20, 2)->default(0);
$table->decimal('charges', 20, 2)->default(0);
$table->decimal('discount', 20, 2)->default(0);
$table->decimal('total', 20, 2)->default(0);
$table->foreign('customer_id')->references('id')
->on('customers')->onDelete('cascade')
->onUpdate('cascade');
$table->foreign('partner_id')->references('id')
->on('partners')->onDelete('cascade')
->onUpdate('cascade');
});
}
public function down()
{
Schema::dropIfExists('orders');
}
}
class CreatePaymentsTable extends Migration
{
public function up()
{
Schema::create('payments', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->integer('order_id')->unsigned();
$table->string('gateway', 100);
$table->string('transaction_id', 100);
$table->decimal('amount', 20, 2);
$table->string('status', 20)->default(Payment::getDefaultStatus());
$table->string('comments', 2000)->nullable();
$table->foreign('order_id')->references('id')
->on('orders')->onDelete('set null')
->onUpdate('cascade');
});
}
public function down()
{
Schema::dropIfExists('payments');
}
}
私が取得エラーです:私は2つのテーブルorders
とpayments
間の関係を確立しようとしている、その移行は次のように定義されている私も、テーブルエンジンことを確認した
[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `payments` add constraint `payments_order_id_foreign` foreign key (`order_id`) references `orders` (`id`) on delete set null on update cascade)
、列型、文字セットなど、同じです(以下show create
の出力である):
| orders | CREATE TABLE `orders` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
`customer_id` int(10) unsigned NOT NULL,
`partner_id` int(10) unsigned NOT NULL,
`status` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'created',
`paid` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'no',
`visitation_charges` decimal(20,2) NOT NULL DEFAULT '0.00',
`taxes` decimal(20,2) NOT NULL DEFAULT '0.00',
`charges` decimal(20,2) NOT NULL DEFAULT '0.00',
`discount` decimal(20,2) NOT NULL DEFAULT '0.00',
`total` decimal(20,2) NOT NULL DEFAULT '0.00',
PRIMARY KEY (`id`),
KEY `orders_customer_id_foreign` (`customer_id`),
KEY `orders_partner_id_foreign` (`partner_id`),
CONSTRAINT `orders_customer_id_foreign` FOREIGN KEY (`customer_id`) REFERENCES `customers` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `orders_partner_id_foreign` FOREIGN KEY (`partner_id`) REFERENCES `partners` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
| payments | CREATE TABLE `payments` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
`order_id` int(10) unsigned NOT NULL,
`gateway` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
`transaction_id` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
`amount` decimal(20,2) NOT NULL,
`status` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'pending',
`comments` varchar(2000) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
ない私が間違っているつもりだ場所がわから。 :/
'payments'に' order_id'が存在しないレコードが 'orders'テーブルに存在するレコードがありますか?これが私がチェックする最初のことです。 –
お支払いの前に注文表が作成されていますか?または、order_idが注文テーブルに存在しない支払いのレコードが存在する可能性があります。 –
@TimBiegeleisenそれは今解決されました。コメントしてくれてありがとう。 :-) – dotslash