Why artisan throws sql "table already exists" on a non existing table - laravel

This is my migration file
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateSessionsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('sessions', function (Blueprint $table) {
$table->string('id')->unique();
$table->text('payload');
$table->integer('last_activity');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('sessions');
}
}
It was created when i ran
php artisan session:table
The database connection is correctly configured since the project is writing and reading from it without problem.
From the root directory of my project i run
php artisan migrate
And i get the following error messages
[Illuminate\Database\QueryException]
SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'sessions' already exists (SQL: create table `sessions` (`id` varchar(255) not null, `payload` text not null, `last_activity` int not null) default character set utf8 collate utf8_unicode_ci)
[PDOException]
SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'sessions' already exists
I know i should run
composer dump-autoload
But i've tried with and without it and i got the same results.
I've also tried the Blueprint type before the $table argument.
It bahaves as if it was accessing some different database than the one it uses when i ran the project

Delete the migration if you haven't added data in it. Or do php artisan migrate:rollback.Then php artisan config:clear and then restart your server. Then rerun the migration

I was able to solve this issue with:
php artisan migrate:fresh

Related

Unable To create new table in database

I created two tables in database. But now I get error to create another one. Third table can not add in the database. But migrate successfully.
I tried all those thinks which are mentioned below:
php artisan make:migration create_book_table
php artisan migrate
after that I get an error:
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes (SQL: alter table `Admins` add unique `admins_emai
l_unique`(`email`))
Then I tried:
php artisan migrate:refresh
but it's still not fixed.
Here is the migration file
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateBookTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('books', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email');
$table->string('phone-number');
$table->string('checkIn');
$table->string('checkOut');
$table->string('Room');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('books');
}
}
Put this line of code in your AppServiceProvider.php file in the boot function:
use Illuminate\Support\Facades\Schema;
public function boot()
{
Schema::defaultStringLength(191);
}

Base table or view not found during migration

I am running exiting laravel project in homestead. When run php artisan migrate get the error.
Here is full error.
In Connection.php line 664:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'forum.chanel' doesn't exist (SQL: select * from `chanel`)
In Connection.php line 326:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'forum.chanel' doesn't exist
This is my chanel table
public function up()
{
Schema::create('chanels', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->string('slug');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('chanels');
}
Why i getting the error and how can i solve this error?
Get the error when run composer update
You need to check your migration table to see if this migration was already run in the past, it is commong that developers change the code after they run the migration.
Or, you might have some even / interception that is running the query select * from chanel just before the migration running, and it making the migration to fail.
It looks like you're model name and table name are not in sync,
Before this remove all tables from DB it could be problem or migration arrangement in migrations table run composer dumpa then
Try updating your model by specifying $table name,
class Chanel extends Model{
public $table = "chanels";
First need to fresh migration type below command
php artisan migrate:fresh
then run new model with migration file with below command
php artisan make:model chanel -m
with this command auto create a migration file and model file
edit migration file location in {your app}\database\migrations
$table->string('title');
$table->string('slug');
added above line into public function up(){ // code }
now run migration type below code
php artisan migrate
I this this will help you

Laravel phpunit tests will not migrate properly

I have 2 tables, badges and counselors. All of my tests were green. I added a third table, a pivot table, named badge_counselor. Here is the migration:
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateBadgeCounselorTable extends Migration {
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('badge_counselor', function (Blueprint $table) {
$table->increments('id');
$table->integer('badge_id')->unsigned();
$table->integer('counselor_id')->unsigned();
$table->foreign('badge_id')->references('id')->on('badges')->onDelete('cascade');
$table->foreign('counselor_id')->references('id')->on('counselors')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('badge_counselor');
}
}
When i run php artisan migrate / php artisan migrate:refresh / php artisan migrate:rollback, everything works fine. Howevever, when i run my unit tests, ALL of them fail. And each one returns the error message:
Illuminate\Database\QueryException: SQLSTATE[HY000]: General error: 1 table "badge_counselor" already exists (SQL: create table "badge_counselor" ("id" integer not null primary key autoincrement, "badge_id" integer not null, "counselor_id" integer not null, foreign key("badge_id") references "badges"("id") on delete cascade, foreign key("counselor_id") references "counselors"("id") on delete cascade))
or simply:
PDOException: SQLSTATE[HY000]: General error: 1 table "badge_counselor" already exists
Judging by the error message a assume that the table is not being dropped correctly, but when i run the migrate commands from the terminal they are perfect. I have tried dropping the migrations table, all the tables, and even the entire database and creating it again and nothing seems to work.
Thanks.
Are you using MySQL? If so, probably try deleting the entire database and create it again but make sure that your database is in InnoDB and not MyISAM. For this, change the default_storage_engine to InnoDB in the Variables section in phpMyAdmin.
[Solution] Not exactly sure what was going on but i deleted the sqlite file that i was using for phpunit, then ran them again.

Change tables structures with migration without losing data?

I have read several threads about this but still not exactly my problem.
So in the users table I have a column called role, which is enum type and has two available values: 1 and 2. I set 2 as the default one. Now I want to change it to 1, for example. I created a new migration, ran php artisan migrate and encounter this error:
[Illuminate\DatabaseQueryException]
SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'role' (SQL: alter table `u
sers` add `role` enum('1', '2') not null default '1')
[PDOException]
SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'role'
Here is code in my CreateUsersTable migration file:
$table->enum('role', ['1', '2'])->default('2');
And I did the same in the new UpdateUsersTable migration file:
$table->enum('role', ['1', '2'])->default('1');
And by the way I can not use php artisan migrate:refresh because it will delete all my data. Where am I doing wrong?
$table->enum('role', ['1', '2'])->default('1')->change();
<?php
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Migrations\Migration;
class UpdateUsersTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
DB::statement('ALTER TABLE `users` MODIFY `role` DEFAULT 1;');
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
DB::statement('ALTER TABLE `users` MODIFY `role` DEFAULT 2;');
}
}
Adapted from this answer to another question
In order to do this you have to add ->nullable() or ->default() to every field you add to the migration file.
create a new migration file
php artisan make:migration add_role_to_users_table --table=users
open the created migration file (database\migrations\2021_12_01_050851-add_role_tables.php) and add below code.
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddRoleToUsers extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->enum('role', ['1', '2'])->default('1')->comment('1 - admin, 2 - normal'); //added
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('role'); //added
});
}
}
?>
Now migration refresh
php artisan migrate:refresh --path=/database/migrations/2021_12_01_050851-add_role_tables.php

Laravel 5.2 database migration error occurs when second table migrate

I am using Laravel 5.2. I did create migration file with php artisan make:migration create_users_table command. File created successfully and I did update file with this code:
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->integer('partner_id');
$table->string('social_id');
$table->integer('device_id');
$table->integer('gcm_id');
$table->integer('user_role');
$table->string('social_media');
$table->string('display_name');
$table->string('first_name');
$table->string('last_name');
$table->integer('status');
$table->string('email')->unique();
$table->string('password');
$table->string('profile_pic');
$table->string('gender');
$table->float('height');
$table->float('weight');
$table->string('dob');
$table->bigInteger('mobile');
$table->string('city');
$table->string('state');
$table->integer('zip');
$table->string('country');
$table->float('latitude');
$table->float('longitude');
$table->float('gps_status');
$table->string('favorite_movie');
$table->string('favorite_food');
$table->string('favorite_weather');
$table->integer('login_status');
$table->string('active');
$table->string('token');
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('users');
}
}`
for running migration i use this 'php artisan migrate' command. and error occurs
'Migration table created successfully.
[Illuminate\Database\QueryException] SQLSTATE[HY000]: General
error: 1025 Error on rename of '.\laravel\users' to
'.\laravel#sql2-1d88-70' (errno: 190 - Operation not allowed when
innodb_forced_recov ery > 0) (SQL: alter table users add unique
users_email_unique(email))
[PDOException] SQLSTATE[HY000]: General error: 1025 Error on
rename of '.\laravel\users' to '.\laravel#sql2-1d88-70' (errno: 190 -
Operation not allowed when innodb_forced_recov ery > 0)'
After that I used rollback command php artisan migrate:rollback but message shows nothing to rollback and when I want to create second table the message shows the user table already exist.
I believe that the problem with the rollback is that since the migration hasn't finished correctly, the process to add the migration in the db table (as a final step) has not been triggered and therefore the artisan command cannot revert it. To solve this I would suggest you to manually delete the table from the DB.
The problem thought is on the migration itself, on top of my head I can't remember if a fresh installation of Laravel already comes with a users table, check that first of all. If it does then u need to alter the table instead of creating it. If not, I can;t see any obvious reason for the migration to fail.

Resources