【问题标题】:Undefined table: 7 ERROR: relation "users" does not exist未定义的表:7 错误:关系“用户”不存在
【发布时间】:2022-01-24 09:13:02
【问题描述】:

朋友们,我在使用 postgres 进行 laravel 迁移时遇到以下问题,当我对迁移进行更改时,在这种情况下是用户表,但是我在尝试从键中删除索引时遇到错误,请你帮帮我遇到这个问题。

这是我的迁移代码:

 public function up() {
    Schema::create('users', function (Blueprint $table) {
        $table->id();
        $table->integer('idProfile');
        $table->string('name');
        $table->string('surname');
        $table->string('email')->unique();
        $table->string('photo')->nullable();
        $table->timestamp('email_verified_at')->nullable();
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
    });
}

public function down() {
    Schema::dropIfExists('users');

    Schema::table('users', function (Blueprint $table){
        $table->dropPrimary('id');
        $table->dropIndex('users_pkey');
    });
}

从我的控制台响应:

这些是列出我的索引:

这是最终表的结构:

评论,有待改进的地方我都在听

【问题讨论】:

  • 永远不要使用图像来显示堆栈溢出中的错误或代码。

标签: laravel postgresql eloquent laravel-8


【解决方案1】:

当您运行migrate:refresh 时,您正在运行down 方法。在您的 down 方法中,您正在删除表格,然后尝试对其进行编辑,这就是您获得“用户”不存在的原因。

如果这只是在开发环境中,请在 up 方法中进行更改,并从 down 方法中删除除 dropIFExists() 之外的所有内容。

【讨论】:

  • 感谢朋友为我工作
【解决方案2】:

强烈建议不要更改迁移文件... 如果您需要向表中添加新行(假设您有 mytable 并且您想将 myrow 添加到表中),您可以在终端中写入:

php artisan make:migration add_myrow_to_mytable_table

之后,编辑新添加的迁移文件! 记得在down函数中添加如下代码:

Schema::table('mytable', function (Blueprint $table) {
        $table->dropColumn('myrow');
    });

毕竟,运行:

php artisan migrate

如果您想从表格中删除一列,请按照以下操作: Laravel Migrations - Dropping columns

【讨论】:

  • 感谢小伙伴的建议,我会付诸实践的。
  • @JuanRincon 随时!
猜你喜欢
  • 2017-04-14
  • 1970-01-01
  • 2021-12-20
  • 2020-01-03
  • 2014-10-24
  • 2018-08-19
  • 1970-01-01
  • 1970-01-01
  • 2019-07-19
相关资源
最近更新 更多