【问题标题】:migrate command in laravellaravel 中的迁移命令
【发布时间】:2020-07-17 08:46:29
【问题描述】:

文章表

public function up()
    {
        Schema::create('Articles', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->integer('user_id')->unsigned();
            $table->string('title');
            $table->string('body');
            $table->timestamps();
            $table->foreign('user_id')->references('id')
                ->on('users')->onDelete('cascade');
        });
    }

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

标签表

    public function up()
    {
        Schema::create('tags', function (Blueprint $table)
        {
            $table->bigIncrements('id');
            $table->string('name');
            $table->timestamps();
        });
        Schema::create('article_tag',function (Blueprint $table)
        {

            $table->integer('article_id')->unsigned()->index();
            $table->foreign('article_id')->references('id')->
                on('articles')->onDelete('cascade');

            $table->integer('tag_id')->unsigned()->index();
            $table->foreign('tag_id')->references('id')->
                on('tags')->onDelete('cascade');

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

我想在 phpmyadmin 中创建标签表,但在 php artisan migrate 命令后遇到此错误

错误

`$ php artisan 迁移 迁移:2020_04_01_195718_create_articles_table

Illuminate\Database\QueryException : SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'articles' already exists (SQL: create table Articles (id bigint unsigned not null auto_increment prim ary key, user_id int unsigned not null, title varchar(255) not null, body varchar(255) not null, created_at timestamp null, updated_at timestamp null) 默认字符集 utf8mb4 collat​​e 'utf8mb4_un icode_ci')`

【问题讨论】:

  • 清除迁移表。然后运行“php artisan migrate”命令并显示错误(如果有)。

标签: php mysql laravel


【解决方案1】:

user_id 表尝试此代码

Schema::create('Articles', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->unsignedBigInteger('user_id');
    $table->string('title');
    $table->string('body');
    $table->timestamps();
    $table->foreign('user_id')->references('id')
        ->on('users')->onDelete('cascade');
});

【讨论】:

  • 当我使用 php artisan 时,只迁移文章表,但用户和标签不做
  • 我做到了,但我的表格名称是标签还没有
【解决方案2】:

用于迁移所有表

php artisan migrate:refresh

如果您不想丢失其他表格数据

只需转到 mysql 或 phpmyadmin 然后删除表并从迁移中删除

然后重新运行

php artisan migrate

【讨论】:

    【解决方案3】:

    试试这个

      public function up()
        {
            Schema::create('tags', function (Blueprint $table)
            {
                $table->bigIncrements('id');
                $table->string('name');
                $table->timestamps();
            });
            Schema::create('article_tag',function (Blueprint $table)
            {
    
                $table->unsignedBigInteger('article_id');
                $table->foreign('article_id')->references('id')->
                    on('articles')->onDelete('cascade');
    
                $table->unsignedBigInteger('tag_id');
                $table->foreign('tag_id')->references('id')->
                    on('tags')->onDelete('cascade');
    
                $table->timestamps();
            });
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-15
      • 2020-10-24
      • 2020-09-20
      • 2017-01-08
      • 2016-01-03
      • 2018-10-03
      • 2013-02-05
      • 2020-11-18
      相关资源
      最近更新 更多