如何在laravel5.5中进行迁移?

pdsfdshx  于 2021-06-24  发布在  Mysql
关注(0)|答案(4)|浏览(276)

我用laravel 5.5创建了一个auth项目,并创建了新的迁移,迁移时收到以下错误消息:
在connection.php第647行:

SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' already exists
(SQL: create table `users` (
      `id` int unsigned not null auto_increment primary key,
      `name` varchar(255) not null,
      `username` varchar(255) not null,
      `email` varchar(255) not null,
      `password` varchar(255) not null,
      `remember_token` varchar(100) null,
      `created_at` timestamp null,
      `updated_at` timestamp null,
      `role` int not null
      ) default character set utf8mb4 collate utf8mb4_unicode_ci
)

在connection.php第449行:

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

我尝试了php artisan迁移——force和php artisanmigrate:rollback
并尝试删除所有选项卡并再次迁移,但仍然存在此错误

niknxzdl

niknxzdl1#

听起来你使用的数据库已经有 users table,但不是 migration table。因此,当您运行迁移时,它试图创建 users 又是一张table。
有两件事你可以尝试:
1) 尝试在mysql中设置一个新的(空的)数据库,并更新您的数据库中的设置 .env 指向此新数据库的文件。跑 php artisan migrate 并查看是否正确应用了迁移。
2) 使用现有数据库,删除/删除所有表(如果要保留数据,请对其进行备份),然后运行 php artisan migrate 看看这能不能解决问题。

hyrbngr7

hyrbngr72#

是否为表“users”创建了新的迁移?
默认情况下,创建新项目时,laravel将为users表生成迁移。这意味着,如果使用“users”表创建新的迁移,它将尝试创建该表两次,从而导致错误。
您可以删除laravel创建的迁移,更改新表的名称,或者修改laravel迁移,而不是创建自己的迁移。

brccelvz

brccelvz3#

读取cmd(dos)中的错误消息并检查laravel文档后
长度错误我不知道是否有人看到这个错误之前或没有,但当我编辑长度它的工作
我编辑3个迁移如下:-

1 -1- Schema::create('users', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('username')->unique(); $table->string('email')->unique(); $table->string('password'); $table->rememberToken(); $table->timestamps(); $table->integer('role'); });

现在是

Schema::create('users', function (Blueprint $table) {
        $table->increments('id')->autoIncrement();
        $table->string('name',200);
        $table->string('username',50)->unique();
        $table->string('email',100)->unique();
        $table->string('password',50);
        $table->string('role',50);
        $table->rememberToken();
        $table->timestamps();

    });

第二个是

Schema::create('password_resets', function (Blueprint $table) { $table->string('email')->index(); $table->string('token'); $table->timestamp('created_at')->nullable(); });

现在是:-

Schema::create('passwordreset', function (Blueprint $table) {
        $table->string('email',200)->index();
        $table->string('token',200);
        $table->timestamp('created_at')->nullable();
    });

第三点是:-

3- Schema::create('tweets', function (Blueprint $table) { $table->increments('id'); $table->integer('user_id')->unsigned()->index(); $table->text('text'); $table->timestamps(); });

现在是:-

Schema::create('tweets', function (Blueprint $table) {
        $table->increments('id')->autoIncrement();
        $table->string('user_id',50)->index();
        $table->string('twetts',255);
        $table->timestamps();
    });
pgvzfuti

pgvzfuti4#

要了解问题并找到解决方案,您需要了解迁移是如何工作的。迁移以创建日期为基础工作。无论何时创建迁移,都会将当前时间戳附加到文件名。迁移是根据这个时间戳运行的。现在,laravel创建了两个迁移,名为2014\u 10\u 12\u 000000\u create\u users\u table.php和2014\u 10\u 12\u 100000\u create\u password\u resets\u table.php。根据laravel的规则,无论何时运行迁移,这两个文件都将迁移,但users表已经存在。删除这些文件,并希望您的问题将得到解决。有一件事,如果您使用模式和迁移在laravel中处理数据库,请不要手动删除db。它会给你带来不必要的麻烦。迁移表跟踪您运行的所有迁移,手动删除数据库将打破在迁移表中保留数据的跟踪。因此,下次运行模式时,laravel将无法理解发生了什么,并将创建错误。希望对你有帮助。

相关问题