0

I am following all the steps for adding new column votes to the user table from laravel, still there isn't in the database? Please tell me where is my mistake? Firstly php artisan make:migration add_votes_to_users_table --table=users

public function up()
{
    Schema::table('users', function (Blueprint $table) {
         $table->integer('votes');
    });
}

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

enter image description here

Error from cmd- In Connection.php line 647:

SQLSTATE[42S01] And In Connection.php line 449:

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

Akeedify
  • 23
  • 7
  • are you getting any error messages? try running the the command in verbose mode `php artisan migrate -v`, does it say anything? – Sasa Blagojevic Nov 13 '18 at 08:17
  • In Connection.php line 647: SQLSTATE[42S01] And In Connection.php line 449: SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' already exists – Akeedify Nov 13 '18 at 08:21
  • It looks like it's trying to create the `users` table again, this often happens as a result of bad data in the Laravel's `migrations` table, try running `php artisan migrate:rollback` a few times until your DB is empty, or remove the tables manually, and then try running `php artisan migrate` – Sasa Blagojevic Nov 13 '18 at 08:26
  • ok understood, thanks – Akeedify Nov 13 '18 at 08:27
  • 1
    You can also try `php artisan migrate:fresh` to start a fresh migration from scratch. Warning: this will drop all tables & then migrate. – brombeer Nov 13 '18 at 08:36
  • Still facing some problem – Akeedify Nov 13 '18 at 09:53

3 Answers3

0

I am suspecting that the reason you are getting this errors can come from 2 sides.

1) you created users table yourself without run any migration

2) a migration was interrupted.

If the above does not work try:

php artisan migrate:reset

to rollback all your changes or if you want to move back 1 step at a time you can try:

php artisan migrate:rollback --step=1

Last if you are sure you can start over and you are ok with that you can run:

php artisan migrate:fresh

This will drop all the tables (check the database just to be sure as well and drop yourself any remaining even though i don't think anything will be left, never happened to me) and then run

php artisan migrate 

all over again

pr1nc3
  • 8,108
  • 3
  • 23
  • 36
  • The `Schema::table` does an `ALTER` statement so there is no need for the `Schema::hasTable`, if he was using `Schema::create` then it would make sense – Sasa Blagojevic Nov 13 '18 at 08:44
  • 1
    You are right. My bad. Also in that case the `hasColumn` will not work as the error is coming from trying to create users table. so i think the only solutions are the artisan commands. – pr1nc3 Nov 13 '18 at 08:47
0

Check your data base. the users table already exit or not. if you create manually. you get this type of error. if users table ther you can remove manually from data base.

  • If already there user table in your db. Then only throw this type of exception. can you create new db manually. then change new db name in your env file. now try this command. hope fully migration working fine. – sathyakrishnapalani Nov 13 '18 at 10:12
0

Thanks all of you for your suggestions, Sasa Blagojevic you are right about one thing my database was created in wrong way. Actually main problem arise when I created the database first time that time SQLSTATE[42000] appeared for user table. That why when updating the table previous error was still showing. After solving it there wasn't error now.

Akeedify
  • 23
  • 7