2017-12-31 72 views
1

この悪名高いエラーが発生しており、何が問題になったのか分かりません。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つのテーブルorderspayments間の関係を確立しようとしている、その移行は次のように定義されている私も、テーブルエンジンことを確認した

[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 

ない私が間違っているつもりだ場所がわから。 :/

+0

'payments'に' order_id'が存在しないレコードが 'orders'テーブルに存在するレコードがありますか?これが私がチェックする最初のことです。 –

+0

お支払いの前に注文表が作成されていますか?または、order_idが注文テーブルに存在しない支払いのレコードが存在する可能性があります。 –

+0

@TimBiegeleisenそれは今解決されました。コメントしてくれてありがとう。 :-) – dotslash

答えて

1

NOT NULLと宣言された列には、外部キーON DELETE SET NULLを作成することはできません。

この列にNULLを設定することはできません。

  • 省略ON DELETE SET NULL:あなたは、次のいずれかを行う場合

    外部キーの作品は

    alter table `payments` add constraint `payments_order_id_foreign` 
    foreign key (`order_id`) references `orders` (`id`) on update cascade; 
    
  • ができるように列を変更しますNULL:

    alter table payments modify order_id int(10) unsigned null; 
    
1

itより良いあなたが別の外部キーの移行ビルドする必要があります。エラーはまだあなたが注文と支払いテーブル手動で削除し、削除する必要があります(存在する場合)

php artisan make:migration add_foreign_key_to_payment_table --table=payments

はそう、この移行に外部キーの支払いを追加し、その後、php artisan migrate

を実行しますそれらをマイグレーションテーブルから削除して、それらを再度追加します。

私はこれがあなたに役立つことを願っています。良い一日! :)

1

をorder_id列にNULL可能()を追加します。以下のように。あなたのエラーはなくなります。

class CreatePaymentsTable extends Migration 
{ 
    public function up() 
    { 
     Schema::create('payments', function (Blueprint $table) { 
     // your others columns 
     $table->integer('order_id')->unsigned()->nullable(); 
    } 
} 
関連する問題