简体   繁体   中英

Laravel - Trying to migrate a new table I have created but VS is trying to migrate a table that has already been migrated?

I have created a table 'create_courses_table' but when I try to migrate this new table, I'm getting the error below from a previous table i have already migrated:

Migrating: 2020_03_04_141200_create_role_user_pivot_table

   Illuminate\Database\QueryException 

  SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'role_user' already exists (SQL: create table `role_user` (`user_id` int unsigned not null, `role_id` int unsigned not null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')

  at vendor/laravel/framework/src/Illuminate/Database/Connection.php:669
    665|         // If an exception occurs when attempting to run a query, we'll format the error
    666|         // message to include the bindings with SQL, which will make this exception a
    667|         // lot more helpful to the developer instead of just the database's errors.
    668|         catch (Exception $e) {
  > 669|             throw new QueryException(
    670|                 $query, $this->prepareBindings($bindings), $e
    671|             );
    672|         }
    673| 

      +9 vendor frames 
  10  database/migrations/2020_03_04_141200_create_role_user_pivot_table.php:22
      Illuminate\Support\Facades\Facade::__callStatic("create")

      +22 vendor frames 
  33  artisan:37
      Illuminate\Foundation\Console\Kernel::handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

I assume there is an error with my PivotTable? I'm not quite sure what I have done wrong. The code for it is below:

<?php

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

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

            $table->unsignedInteger('role_id');
            $table->foreign('role_id')->references('id')->on('roles');
        });
    }

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

Thanks, any help is appreciated.

I have created a table 'create_courses_table' but when I try to migrate this new table, I'm getting the error below from a previous table i have already migrated:

Migrating: 2020_03_04_141200_create_role_user_pivot_table

   Illuminate\Database\QueryException 

  SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'role_user' already exists (SQL: create table `role_user` (`user_id` int unsigned not null, `role_id` int unsigned not null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')

  at vendor/laravel/framework/src/Illuminate/Database/Connection.php:669
    665|         // If an exception occurs when attempting to run a query, we'll format the error
    666|         // message to include the bindings with SQL, which will make this exception a
    667|         // lot more helpful to the developer instead of just the database's errors.
    668|         catch (Exception $e) {
  > 669|             throw new QueryException(
    670|                 $query, $this->prepareBindings($bindings), $e
    671|             );
    672|         }
    673| 

      +9 vendor frames 
  10  database/migrations/2020_03_04_141200_create_role_user_pivot_table.php:22
      Illuminate\Support\Facades\Facade::__callStatic("create")

      +22 vendor frames 
  33  artisan:37
      Illuminate\Foundation\Console\Kernel::handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

I assume there is an error with my PivotTable? I'm not quite sure what I have done wrong. The code for it is below:

<?php

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

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

            $table->unsignedInteger('role_id');
            $table->foreign('role_id')->references('id')->on('roles');
        });
    }

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

Thanks, any help is appreciated.

From the comments on your question I assume that something went wrong in storing the migration in the migration table.

Run the following query on your database. Should it return 0, proceed with my answer. Should it return 1 (or even more), please ignore my answer, the error is somewhere else:

SELECT COUNT(*) FROM migrations WHERE migration = '2020_03_04_141200_create_role_user_pivot_table';

To fix this, you could add the migration manually to the table:

Just add a new tuple in the table called migrations , with the filename (without .php , ergo 2020_03_04_141200_create_role_user_pivot_table ) in the migration column and a batch -number that is one higher than your highest value in the table.

Ergo: Do this:

Get the incremented batch number:

SELECT MAX(batch)+1 FROM migrations

And then insert your migration manually (replace 42 with the result of the previous query):

INSERT INTO migrations (migration, batch)
VALUES ('2020_03_04_141200_create_role_user_pivot_table', 42)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM