ProCodeAL
ProCodeAL

Reputation: 23

Laravel 8 SQLSTATE[42S02]: Base table or view not found: 1146 Table even though I am trying to create this table

I am using Laravel 8. I am trying to create a new table and this table does not exist in database and there are not any other table that has the same name. I did the migration and this is the migration code:

<?php

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

class CreateCertificateTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('certificate', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('user_id');
            $table->unsignedBigInteger('vac_id');
            $table->time('last_shot_date');
            $table->timestamps();

            $table->foreign('user_id')->references('id')->on('users');
            $table->foreign('vac_id')->references('id')->on('vaccination');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('certificate', function (Blueprint $table) {
            //
        });
    }
}

after doing

php artisan migrate

This is the full error message I get:

   Illuminate\Database\QueryException

  SQLSTATE[42S02]: Base table or view not found: 1146 Table 'vaccinationappointme
nts.certificate' doesn't exist (SQL: alter table `certificate` add `id` bigint un
signed not null auto_increment primary key, add `user_id` bigint unsigned not nul
l, add `vac_id` bigint unsigned not null, add `last_shot_date` time not null, add
 `created_at` timestamp null, add `updated_at` timestamp null)

  at D:\xampp\htdocs\Vaccination_Appointments\vendor\laravel\framework\src\Illumi
nate\Database\Connection.php:692
    688▕         // If an exception occurs when attempting to run a query, we'll
format the error
    689▕         // message to include the bindings with SQL, which will make thi
s exception a
    690▕         // lot more helpful to the developer instead of just the databas
e's errors.
    691▕         catch (Exception $e) {
  ➜ 692▕             throw new QueryException(
    693▕                 $query, $this->prepareBindings($bindings), $e
    694▕             );
    695▕         }
    696▕

  • A table was not found: You might have forgotten to run your migrations. You c
an run your migrations using `php artisan migrate`.
    https://laravel.com/docs/master/migrations#running-migrations

  1   D:\xampp\htdocs\Vaccination_Appointments\vendor\laravel\framework\src\Illum
inate\Database\Connection.php:485
      PDOException::("SQLSTATE[42S02]: Base table or view not found: 1146 Table '
vaccinationappointments.certificate' doesn't exist")

  2   D:\xampp\htdocs\Vaccination_Appointments\vendor\laravel\framework\src\Illum
inate\Database\Connection.php:485
      PDOStatement::execute()

I have tried to do:

php artisan migrate:refresh

and still the same problem. I have not used Models.

Upvotes: 0

Views: 2612

Answers (1)

Emre Kaya
Emre Kaya

Reputation: 121

if you want to create a table with migration, you need to use ::create() instead of ::table()

::table() function tries to alter your table

Upvotes: 5

Related Questions