【问题标题】:Laravel migrate database database using artisan with new columnsLaravel 使用带有新列的工匠迁移数据库数据库
【发布时间】:2015-04-07 20:55:48
【问题描述】:

我正在使用 laravel,我正在尝试进行另一次迁移,但该表已经存在,因此它会引发以下错误:

[PDOException]
  SQLSTATE[42S01]: Base table or view already 
  exists: 1050 Table 'users' already exists

我已将此行添加到迁移文件中:

$table->softDeletes();

这是完整的文件:

<?php

use Illuminate\Database\Migrations\Migration;

class ConfideSetupUsersTable extends Migration
{
    /**
     * Run the migrations.
     */
    public function up()
    {
        // Creates the users table
        Schema::create('users', function ($table) {
            $table->increments('id');
            $table->string('firstname');
            $table->string('lastname');
            $table->string('username')->unique();
            $table->string('email')->unique();
            $table->string('password');
            $table->string('confirmation_code');
            $table->string('remember_token')->nullable();
            $table->softDeletes();
            $table->boolean('confirmed')->default(false);
            $table->timestamps();
        });

        // Creates password reminders table
        Schema::create('password_reminders', function ($table) {
            $table->string('email');
            $table->string('token');
            $table->timestamp('created_at');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down()
    {
        Schema::drop('password_reminders');
        Schema::drop('users');
    }
}

任何想法我做错了什么?

【问题讨论】:

    标签: laravel database-migration laravel-artisan


    【解决方案1】:

    您可以回滚迁移,然后再次运行:

    php artisan migrate:rollback
    

    然后:

    php artisan migrate
    

    注意在您的情况下,这将删除表中的所有数据,因为它会删除 userspassword 提醒,然后重新创建它。

    另一种方法是创建一个新的迁移:

    php artisan migrate:make users_add_soft_deletes
    

    把它放在那里:

    Schema::table('users', function ($table) {
        $table->softDeletes();
    });
    

    然后运行 ​​php artisan migrate 以应用新的迁移

    【讨论】:

    • 好的,我明白了,替代解决方案是否允许我指定表中的哪个位置?
    • 是的,如果你使用 MySQL,你可以这样做 $table-&gt;softDeletes()-&gt;after('remember_token');
    猜你喜欢
    • 2019-10-10
    • 2021-12-03
    • 1970-01-01
    • 2014-12-15
    • 2021-08-17
    • 2014-06-05
    • 2019-01-19
    • 2018-04-04
    • 2016-07-08
    相关资源
    最近更新 更多