简体   繁体   English

Laravel - 尝试迁移我创建的新表,但 VS 正在尝试迁移已迁移的表?

[英]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:我创建了一个表“create_courses_table”,但是当我尝试迁移这个新表时,我从我已经迁移的上一个表中收到以下错误:

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:我已经创建了一个表“ create_courses_table”,但是当我尝试迁移这个新表时,我从已经迁移的上一个表中得到了以下错误:

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.如果它返回 0,请继续我的回答。 Should it return 1 (or even more), please ignore my answer, the error is somewhere else:如果它返回 1(甚至更多),请忽略我的回答,错误在其他地方:

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.只需在名为migrations的表中添加一个2020_03_04_141200_create_role_user_pivot_table ,在migration列中使用文件名(没有.php ,ergo 2020_03_04_141200_create_role_user_pivot_table )和一个比表中最高值高一个的batch

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):然后手动插入您的迁移(用上一个查询的结果替换42 ):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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