Home > Software design >  Undefined table: 7 ERROR: relation "users" does not exist
Undefined table: 7 ERROR: relation "users" does not exist

Time:01-24

Friends I have the following problem with laravel migrations using postgres, and when I make changes to a migration, in this case the users table, but I get an error trying to remove an index from a key, can you help me please with this problem.

This is my migration code:

 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');
    });
}

response from my console:

enter image description here

These are the indices that list me:

enter image description here

This is the structure of the final table:

enter image description here

Comments, things to improve I am all ears

CodePudding user response:

When you are running migrate:refresh, you are running the down method. In your down method, you are dropping the table, and then trying to make edits to it, which is why you are getting "users" doesn't exist.

If this is just in a development env, make your changes in the up method, and remove everything apart from dropIFExists() from the down method.

CodePudding user response:

It is highly recommended that don't change the migration file... If you need to add a new row to your table (Consider you have mytable and you want to add myrow to the table), you can write in terminal :

php artisan make:migration add_myrow_to_mytable_table

after that , edit new added migration file! remember to add the following code in down function :

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

after all, run :

php artisan migrate

If you want to remove a column from your table just follow this one : Laravel Migrations - Dropping columns

  •  Tags:  
  • Related