Reputation: 405
I have a order table for my orders, i created this table before, but after some times i have to change my migration.
This is my orders table before changes :
Schema::create('orders', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->bigInteger('price');
$table->enum('status', ['unpaid', 'paid', 'preparation', 'posted', 'recieved', 'canceled']);
$table->string('tracking_serial')->nullable();
$table->timestamps();
});
Schema::create('order_product', function (Blueprint $table) {
$table->unsignedBigInteger('product_id');
$table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
$table->unsignedBigInteger('order_id');
$table->foreign('order_id')->references('id')->on('orders')->onDelete('cascade');
$table->integer('quantity');
$table->primary(['product_id', 'order_id']);
});
And this is orders table after changes :
Schema::create('orders', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->unsignedBigInteger('address_id');
$table->foreign('address_id')->references('id')->on('addresses')->onDelete('cascade');
$table->bigInteger('price');
$table->string('post_type');
$table->enum('status', ['unpaid', 'paid', 'preparation', 'posted', 'recieved', 'canceled']);
$table->string('tracking_serial')->nullable();
$table->primary(['user_id', 'address_id']);
$table->timestamps();
});
Schema::create('order_product', function (Blueprint $table) {
$table->unsignedBigInteger('product_id');
$table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
$table->unsignedBigInteger('order_id');
$table->foreign('order_id')->references('id')->on('orders')->onDelete('cascade');
$table->integer('quantity');
$table->primary(['product_id', 'order_id']);
});
as you can see i import 3 new lines in orders
schema :
$table->unsignedBigInteger('address_id');
$table->foreign('address_id')->references('id')->on('addresses')->onDelete('cascade');
$table->string('post_type');
$table->primary(['user_id', 'address_id']);
But when i want to run php artisan migrate
i get this error :
SQLSTATE[HY000]: General error: 1005 Can't create table `shop`.`orders` (errno: 150 "Foreign key
constraint is incorrectly formed") (SQL: alter table `orders` add constraint
`orders_address_id_foreign` foreign key (`address_id`) references `addresses` (`id`) on delete
cascade)
Why do I have this error ?
UPDATE :
This is my addresses table :
Schema::create('addresses', function (Blueprint $table) {
$table->id();
$table->string('state');
$table->string('city');
$table->text('address');
$table->integer('plaque');
$table->string('postal');
$table->timestamps();
});
Schema::create('address_user', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users')
->onDelete('cascade');
$table->unsignedBigInteger('address_id');
$table->foreign('address_id')->references('id')->on('addresses')
->onDelete('cascade');
$table->primary(['user_id', 'address_id']);
$table->timestamps();
});
And order migrate before address.
Upvotes: 5
Views: 1966
Reputation: 1618
You must first create the table, then create the foreign keys:
Schema::create('orders', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('user_id');
$table->unsignedBigInteger('address_id');
$table->bigInteger('price');
$table->string('post_type');
$table->enum('status',['unpaid','paid','preparation','posted', 'recieved', 'canceled']);
$table->string('tracking_serial')->nullable();
$table->primary(['user_id', 'address_id']);
$table->timestamps();
});
Schema::table('orders', function (Blueprint $table) {
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('address_id')->references('id')->on('addresses')->onDelete('cascade');
});
Upvotes: 3
Reputation: 34
i hope you have used a migration to change the order table and not just modified the original migration?
this error generally happens for few reasons:
Upvotes: 2