-1

I am working on laravel 9. Actually i want to create 2 tables in my database. I saved and next run 'php artisan migrate' in terminal and it pops up error:

SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'orders' already exists 
(SQL: create table `orders` (`id` int unsigned not null auto_increment primary key, 
`order_number` varchar(255) not null, `user_id` int unsigned not null, `status` 
enum('pending', 'processing', 'completed', 'decline') not null default 'pending', 
`grand_total` decimal(20, 6) not null, `item_count` int unsigned not null, `payment_status` 
 tinyint(1) not null default '1', `payment_method` varchar(255) null, `first_name` 
 varchar(255) not null, `last_name` varchar(255) not null, `address` text not null, `city` 
 varchar(255) not null, `country` varchar(255) not null, `post_code` varchar(255) not null, 
 `phone_number` varchar(255) not null, `notes` text null, `created_at` timestamp null, 
 `updated_at` timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')

2022_08_26_082806_create_order_items_table.php

     <?php
     use Illuminate\Support\Facades\Schema;
     use Illuminate\Database\Schema\Blueprint;
     use Illuminate\Database\Migrations\Migration;

     class CreateOrderItemsTable extends Migration
     {
/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::dropIfExists('order_items');
    Schema::create('order_items', function (Blueprint $table) {
        $table->increments('id');
        $table->unsignedBigInteger('order_id')->index();
        $table->unsignedBigInteger('product_id')->index();
        $table->unsignedInteger('quantity');
        $table->decimal('price', 20, 6);

        $table->foreign('order_id')->references('id')->on('orders')->onDelete('cascade');
        $table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');

        $table->timestamps();
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::dropIfExists('order_items');
}
}

2022_08_26_082629_create_orders_table.php

                  <?php

                 use Illuminate\Support\Facades\Schema;
                 use Illuminate\Database\Schema\Blueprint;
                 use Illuminate\Database\Migrations\Migration;

         class CreateOrdersTable extends Migration
      {
/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::create('orders', function (Blueprint $table) {
        $table->increments('id');
        $table->string('order_number')->unique();
        $table->unsignedInteger('user_id');
        $table->foreign('user_id')->references('id')->on('users');

        $table->enum('status', ['pending', 'processing', 'completed', 'decline'])->default('pending');
        $table->decimal('grand_total', 20, 6);
        $table->unsignedInteger('item_count');

        $table->boolean('payment_status')->default(1);
        $table->string('payment_method')->nullable();

        $table->string('first_name');
        $table->string('last_name');
        $table->text('address');
        $table->string('city');
        $table->string('country');
        $table->string('post_code');
        $table->string('phone_number');
        $table->text('notes')->nullable();

        $table->timestamps();
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::dropIfExists('orders');
}
}

I checked my database and there is 'orders' table but not 'order_items'. Please help me someone!

Peter Peter
  • 21
  • 1
  • 4
  • 1
    *1050 Table 'orders' already exists*. If the tables exists, why you are wondering that you get an error that the table already exists? – Jens Aug 26 '22 at 09:23
  • i want to create also 'order_items' table, and when i run php artisan migrate it gives me this error – Peter Peter Aug 26 '22 at 09:24
  • You should change the creation of table `order` to `create if not exists` – Jens Aug 26 '22 at 09:26
  • how? please say to me – Peter Peter Aug 26 '22 at 09:27
  • You are probably executing the script that tries to create the orders table as wel, hence the error message. – Shadow Aug 26 '22 at 09:28
  • i have orders table in database, now i want to create order_items in database – Peter Peter Aug 26 '22 at 09:28
  • and i have error code, i do not know why it is – Peter Peter Aug 26 '22 at 09:28
  • @Shadow, what should i do? – Peter Peter Aug 26 '22 at 09:29
  • Does this answer your question? [Schema Builder : Create Table if not exists](https://stackoverflow.com/questions/35467605/schema-builder-create-table-if-not-exists) – Jens Aug 26 '22 at 09:40
  • @Jens, actually i really want not to run php artisan migrate:fresh and next delete all tables, and this is not answer of my question, please help, actually please check order_items table – Peter Peter Aug 26 '22 at 09:47
  • Your `order_items`table is not the problem. The problem is that you try to recreate the `order` table. If you add `if (!Schema::hasTable('order')) {` the order table will not be created and you should be able to create `order_items` – Jens Aug 26 '22 at 09:50
  • @Jens, i add this in order_items? – Peter Peter Aug 26 '22 at 09:51
  • No in `order`.. – Jens Aug 26 '22 at 09:51
  • Does this answer your question? [SQLSTATE\[42S01\]: Base table or view already exists or Base table or view already exists: 1050 Table](https://stackoverflow.com/questions/44141475/sqlstate42s01-base-table-or-view-already-exists-or-base-table-or-view-already) – discussion Aug 26 '22 at 10:29

2 Answers2

0

Because you cannot create a already exiting table like orders, if you JUST need to migrate order_items table run this command

php artisan migrate --path=database/migrations/2022_08_26_082806_create_order_items_table.php

If you need to update the orders table alongside of the above migration I recommend running the refresh command and reset all of your tables like so

php artisan migrate:fresh
dz0nika
  • 882
  • 1
  • 5
  • 16
0

This question has been answered correctly with steps to follow here. Follow this link Base table or view already exists: Laravel...

  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/33836949) – Xypron Feb 17 '23 at 17:57