2

when i run php artisan migrate i keep getting this error:

[Illuminate\Database\QueryException]
  SQLSTATE[HY000]: General error: 1005 Can't create table 'laracart.#sql-25b_
  3' (errno: 150) (SQL: alter table `customers_orders` add constraint custome
  rs_orders_products_id_foreign foreign key (`products_id`) references `produ
  cts` (`id`) on delete cascade on update cascade)

here is the schema for customers order:

Schema::create('customers_orders', function(Blueprint $table)
        {
            $table->engine = 'InnoDB';

            $table->increments('id');
            $table->integer('customers_id')->unsigned();
            $table->integer('products_id')->unsigned();

            $table->foreign('customers_id')
                  ->references('id')
                  ->on('customers')
                  ->onDelete('cascade')
                  ->onUpdate('cascade');

            $table->foreign('products_id')
                  ->references('id')
                  ->on('products')
                  ->onDelete('cascade')
                  ->onUpdate('cascade');

            $table->integer('quantity')->unsigned();
            $table->float('unit_price');
            $table->float('subtotal');
            $table->string('shipping_price');
            $table->float('grand_total');
            $table->string('status');
            $table->string('tracking_number');
            $table->string('payment_reference');

            $table->timestamps();
            $table->integer('is_deleted')->default('0');
            $table->softDeletes();
        });

and the schema for products

Schema::create('products', function(Blueprint $table) { $table->engine = 'InnoDB';

        $table->increments('id');
        $table->integer('categories_id')->unsigned();
        $table->foreign('categories_id')
              ->references('id')
              ->on('categories')
              ->onDelete('cascade')
              ->onUpdate('cascade');

        $table->string('name')->unique();
        $table->text('description');
        $table->string('sku')->nullable();
        $table->float('price')->default('0.00');
        $table->float('weight')->default('0');
        $table->text('meta_title')->nullable();
        $table->text('meta_description')->nullable();
        $table->integer('is_visible')->default('1');

        $table->timestamps();
        $table->integer('is_deleted')->default('0');
        $table->softDeletes();
    });
Exploit
  • 6,278
  • 19
  • 70
  • 103
  • 1
    foreign keys wreak havoc on trying to update a table. I usually write scripts that drop all the tables and recreate them after I change the mapping. You can't alter a table/field that is foreign-keyed to another table, because you would break the relationship. Its a pain the $$$ but necessary. – Rottingham Jan 07 '14 at 23:13
  • possible duplicate of [MySQL Creating tables with Foreign Keys giving errno: 150](http://stackoverflow.com/questions/1457305/mysql-creating-tables-with-foreign-keys-giving-errno-150) (check all those reasons why you can get it, a first simple one would be: you _do_ make `products` _before_ `customers_orders` I hope? Why is product_id unsigned, and not `products.id`? (at least, I don't see it)) – Wrikken Jan 07 '14 at 23:15
  • i found the problem, the customer_orders migration was ahead of products creation, so i had to switch places and it worked. – Exploit Jan 07 '14 at 23:51

0 Answers0