I am trying do to a very basic migration. When i run php artisan migrate i get this error:
Migrating: 2019_12_14_000001_create_personal_access_tokens_table
Illuminate\Database\QueryException
SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'personal_access_tokens' already exists (SQL: create tab....///
As you can see, I dont have that "2019_12_14_000001_create_personal_access_tokens_table" migration in my migrations table.
I have done the following:
php artisan cache:clear
php artisan config:clear
./composer dump-autoload
And im still getting this ghost migration. What should i do?
The contents of the create_file_table file I am trying to run is:
public function up()
{
Schema::create('files', function (Blueprint $table) {
$table->id();
$table->string('quoteNumber');
$table->string('purchaseOrderNumber');
$table->string('name')->nullable();
$table->string('file_path')->nullable();
$table->timestamps();
});
}
CodePudding user response:
As the error indicates, the table exists.
php artisan cache:clear
php artisan config:clear
./composer dump-autoload
Will not work. Why? Because laravel checks against the migration table and not the cache or config.
Amongst the solutions, you have:
artisan migrate:freshwhich will drop all tables from the database and then execute the migrate command- In the
migrationstable, manually delete the record for the migration. And delete the instance of the table.
The screenshot you have shared is from the code, not the database. If you refer to the database, you will find a record with that name.
Why does this happen?
Migrations run in batches. When a migration file is deleted after the table is created, the table will still exist. The appropriate way is to rollback that migration. As such, just because the file does not exist, doesn't mean the table doesn't exist.
CodePudding user response:
2019_12_14_000001_create_personal_access_tokens_table
Is part of Laravel Sanctum.
You can publish it to your migrations folder by running the following command.
php artisan vendor:publish --provider="Laravel\Sanctum\SanctumServiceProvider"
Otherwise it stays with the package and you will not see it in your migrations folder.
If you would like to ignore it you can follow the documentation included below.
Migration Customization
If you are not going to use Sanctum's default migrations, you should call the
Sanctum::ignoreMigrationsmethod in theregistermethod of yourApp\Providers\AppServiceProviderclass. You may export the default migrations by executing the following command:
php artisan vendor:publish --tag=sanctum-migrations

