【问题标题】:Laravel issue on migrationLaravel 关于迁移的问题
【发布时间】:2017-09-19 20:57:45
【问题描述】:

每当我进行新的迁移并尝试迁移它时,我得到的结果就是这样。即使我尝试迁移:刷新等等。你觉得这里的问题是什么?我还检查了我的 .env 文件。谢谢!

[照亮\数据库\查询异常] SQLSTATE[42S01]:基表或视图已存在:1050 表“用户”已存在(SQL:创建表 users ( id int unsigned not null auto_increment 主键,name varchar(255) not null,@ 987654325@ varchar(255) 不为空, password varchar(255) not null, remember_token varchar(100) null, created_at timestamp null, updated_at tim estamp null) 默认字符集 utf8mb4 collat​​e utf8mb4_unicode_ci)

[PDO异常] SQLSTATE[42S01]:基表或视图已存在:1050 表“用户”已存在

【问题讨论】:

标签: php mysql sql-server laravel migrate


【解决方案1】:

使用!Schema::hasTable('users'),它将检查数据库中是否已经存在表。

如果您不想删除已经存在的表,那么这是个好主意。

if (!Schema::hasTable('users')) {
    Schema::create('users', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('name');
        $table->string('email')->unique();
        $table->timestamp('email_verified_at')->nullable();
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
    });
}

【讨论】:

    【解决方案2】:

    问题是你试图创建一个已经存在于数据库中的表,

    在运行php artisan migrate:refresh之前从数据库中删除用户表

    或者,如果您进行了另一次迁移以将新列添加到用户表中,那么您需要拥有

    Schema::table('users', function (Blueprint $table)

    代替

    Schema::create('users', function (Blueprint $table)

    重要的是,Schema::create 不会创建表,Schema::table 是您在修改现有表时使用的对象。

    【讨论】:

    • 我忘了包括我要在数据库中迁移的不是“用户”表。但是每当我使用 php artisan migrate 时,它​​不会迁移我想要包含在数据库中的新表,而是它总是给我像这个一样的结果。
    • 如果您运行php artisan migrate:status,您可以查找旁边带有 (N) 的尚未迁移的迁移文件。在您的情况下,当错误是因为表已经存在时,您可以注释掉迁移文件中的行,或者如果您要添加新列,请确保您没有 Schema::create('tableName', function (Blueprint $table) 两次。
    猜你喜欢
    • 1970-01-01
    • 2016-09-17
    • 2017-07-14
    • 2017-01-22
    • 2021-08-06
    • 2021-10-29
    • 2018-04-26
    • 1970-01-01
    相关资源
    最近更新 更多