1

I am learning migration in Laravel. I have created a migration file and migrations seems to work fine from command line. But inside my mysql file the username column is not created after migration.

>php artisan make:migration add_username_to_users_table
 Created Migration: 2022_07_15_123530_add_username_to_users_table
>php artisan migrate 
 Migrating: 2022_07_15_123530_add_username_to_users_table 
 Migrated:  2022_07_15_123530_add_username_to_users_table (31.34ms)

Migration file:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->string('username');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->dropColumn('username');
        });
    }
};

users table in mysql database: No username column created

MariaDB [julai_pro]> describe users;
+-------------------+---------------------+------+-----+---------+----------------+
| Field             | Type                | Null | Key | Default | Extra          |
+-------------------+---------------------+------+-----+---------+----------------+
| id                | bigint(20) unsigned | NO   | PRI | NULL    | auto_increment |
| name              | varchar(255)        | NO   |     | NULL    |                |
| email             | varchar(255)        | NO   | UNI | NULL    |                |
| email_verified_at | timestamp           | YES  |     | NULL    |                |
| password          | varchar(255)        | NO   |     | NULL    |                |
| remember_token    | varchar(100)        | YES  |     | NULL    |                |
| created_at        | timestamp           | YES  |     | NULL    |                |
| updated_at        | timestamp           | YES  |     | NULL    |                |
+-------------------+---------------------+------+-----+---------+----------------+
8 rows in set (1.265 sec)

And when refresh my migration it gives me the below error

>php artisan migrate:refresh 
 Rolling back: 2022_07_15_123530_add_username_to_users_table

   Illuminate\Database\QueryException

  SQLSTATE[42000]: Syntax error or access violation: 1091 Can't DROP COLUMN `username`; check that it exists (SQL: alter table `users` drop `username`)

 
-app\vendor\laravel\framework\src\Illuminate\Database\Connection.php:759
    755▕         // If an exception occurs when attempting to run a query, we'll format the error
    756▕         // message to include the bindings with SQL, which will make this exception a
    757▕         // lot more helpful to the developer instead of just the database's errors.
    758▕         catch (Exception $e) {   ➜ 759▕             throw new QueryException(
    760▕                 $query, $this->prepareBindings($bindings), $e
    761▕             );
    762▕         }
    763▕     }

  1  
-app\vendor\laravel\framework\src\Illuminate\Database\Connection.php:544
      PDOException::("SQLSTATE[42000]: Syntax error or access violation: 1091 Can't DROP COLUMN `username`; check that it exists")

  2  
-app\vendor\laravel\framework\src\Illuminate\Database\Connection.php:544
      PDOStatement::execute()

>php artisan migrate 
 Nothing to migrate.

Option1 : I tried to solve this with the below command with success.

>php artisan cache:clear
Application cache cleared successfully.

>php artisan config:cache
Configuration cache cleared successfully.
Configuration cached successfully.

Option 2 : I also tried the option from @Udhav Sarvaiya

Open your app/Providers/AppServiceProvider.php file and inside the boot() method set a default string length:

use Illuminate\Support\Facades\Schema;

public function boot()
{
    Schema::defaultStringLength(191);
}

None of the options solved the problem the username Column is not created in users table. Kindly assist.

Edit: the migration table in database

MariaDB [julai_pro]> select * from migrations;
+----+-------------------------------------------------------+-------+
| id | migration                                             | batch |
+----+-------------------------------------------------------+-------+
|  1 | 2014_10_12_000000_create_users_table                  |     1 |
|  2 | 2014_10_12_100000_create_password_resets_table        |     1 |
|  3 | 2019_08_19_000000_create_failed_jobs_table            |     1 |
|  4 | 2019_12_14_000001_create_personal_access_tokens_table |     1 |
|  5 | 2022_07_15_123530_add_username_to_users_table         |     2 |
+----+-------------------------------------------------------+-------+
5 rows in set (0.056 sec)
Gabriel Rogath
  • 710
  • 2
  • 8
  • 23
  • @RawSlugs the issue is that in `users` table the `username` column is not added as expected – Gabriel Rogath Jul 20 '22 at 16:16
  • 1
    you can try commenting out the contents of the down method, run ```php artisan migrate:refresh``` then uncomment it and try again. if this works then you probably just forgot to save before the first ```php artisan migrate``` – RawSlugs Jul 20 '22 at 16:21
  • @RawSlugs let me do this and see – Gabriel Rogath Jul 20 '22 at 16:27
  • @RawSlugs it worked. I did as suggested commenting out but with only one command `php artisan migrate:refresh` it Rolling back all the migration and started the migrations of each file automatically without `php artisan migrate` command and `username` column was created in `users` table. This means I truly forgot to save the file after editing and I started migrating. – Gabriel Rogath Jul 20 '22 at 16:38
  • @RawSlugs can you put this in your answer, I need to mark it as accepted answer. thanks in advance. – Gabriel Rogath Jul 20 '22 at 16:45

1 Answers1

1

php artisan migrate:refresh first rolls back all your migrations then runs it again you would want to do php artisan migrate to add a migration and php artisan migrate:fresh if you would like it to wipe the database first

> php artisan migrate
Nothing to migrate.

you can look in the migrations table to see which migrations ran

RawSlugs
  • 520
  • 4
  • 11
  • just added the migrations table on edit of question – Gabriel Rogath Jul 20 '22 at 15:50
  • before I wipe the database, I would like to know what causes the problem for learning the. Thanks for the tip in advance. – Gabriel Rogath Jul 20 '22 at 15:54
  • 1
    @GabrielRogath well when running ```php artisan migrate:refresh``` laravel will run the ```down``` method on every migration in your migrations folder. the problem you are having is that you never ran ```php artisan migrate``` so when this migration file was being rolled back it was before the column was ever created (before the ```up``` method was ever called) – RawSlugs Jul 20 '22 at 16:06
  • 1
    Another thing to consider: when adding a column to a table with existing rows you may need to make it nullable or add a default value – RawSlugs Jul 20 '22 at 16:07
  • if you check well at the top of my question after creating the migrate with `php artisan make:migration add_username_to_users_table` it follows with `php artisan migrate` command which was successfully. – Gabriel Rogath Jul 20 '22 at 16:12
  • 1
    actually thinking about it, it seems kind of odd that laravel is running the down method without it being in the database. i don't think this is the case. can you reproduce the error? what i think may have happened (has happened to me in the past) you created a migration file with ```artisan``` then edited the file adding your column to the ```up``` method you then ran (probably forgot to save) then ran ```php artisan migrate``` (if you didnt save then the up method would be empty) then at some point before running ```php artisan migrate:refresh``` you probably saved... – RawSlugs Jul 20 '22 at 16:18