Laravel migrate database database using artisan with new columns - laravel

I am using laravel and I am trying to do another migration but the table already exists so it is throwing the following error:
[PDOException]
SQLSTATE[42S01]: Base table or view already
exists: 1050 Table 'users' already exists
I've added this line into the migration file:
$table->softDeletes();
This is the full file:
<?php
use Illuminate\Database\Migrations\Migration;
class ConfideSetupUsersTable extends Migration
{
/**
* Run the migrations.
*/
public function up()
{
// Creates the users table
Schema::create('users', function ($table) {
$table->increments('id');
$table->string('firstname');
$table->string('lastname');
$table->string('username')->unique();
$table->string('email')->unique();
$table->string('password');
$table->string('confirmation_code');
$table->string('remember_token')->nullable();
$table->softDeletes();
$table->boolean('confirmed')->default(false);
$table->timestamps();
});
// Creates password reminders table
Schema::create('password_reminders', function ($table) {
$table->string('email');
$table->string('token');
$table->timestamp('created_at');
});
}
/**
* Reverse the migrations.
*/
public function down()
{
Schema::drop('password_reminders');
Schema::drop('users');
}
}
Any ideas what I'm doing wrong?

You can rollback the migration and then run it again:
php artisan migrate:rollback
And then:
php artisan migrate
Attention in your case this will delete all data from the table since it will drop users and password reminder and then recreate it.
The alternative is to create a new migration:
php artisan migrate:make users_add_soft_deletes
Put this in there:
Schema::table('users', function ($table) {
$table->softDeletes();
});
And run php artisan migrate to apply the new migration

Related

Laravel: 1060 Duplicate column name while running migrations

I am getting the following error when running migrations:
PDOException::("SQLSTATE[42S21]: Column already exists: 1060 Duplicate
column name 'role_id'")
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
if(!Schema::hasTable('users')) {
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
Schema::table('users', function (Blueprint $table) {
$table->integer('role_id')->unsigned();
$table->string('first_name')->nullable();
$table->string('middle_name')->nullable();
$table->string('last_name')->nullable();
$table->string('city')->nullable();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('users');
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('role_id');
});
}
}
I have deleted most of the migrated tables as it gives duplication issues. Could that be related to my existing problem ?
try running these commands in your terminal:
composer dump-autoload // updates whatever you changed in your migration
php artisan migrate:fresh // migrates migration from the start
if these doesn't not work, post your column structure so we can understand more about your problem.

rename column in laravel using migration [duplicate]

This question already has answers here:
Laravel migrations: Class "not found"
(14 answers)
Closed 3 months ago.
I use laravel 5.6
I want to change author_ID to user_id
I have columns as mentioned below:
class CreatePostsTable extends Migration{
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->bigInteger('author_ID');
$table->text('title');
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('posts');
}
}
I use the below link to change my column name :
How can I rename a column in laravel using migration?
then create blow migration :
php artisan make:migration rename_author_id_in_post_table
rename migration file :
class UpdateUsernameInPostTable extends Migration{
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->renameColumn('author_ID', 'user_id');
});
}
public function down()
{
Schema::create('posts', function (Blueprint $table) {
$table->renameColumn('user_id', 'author_ID');
});
}
}
but with the run command : php artisan migrate I have the below error :
Symfony\Component\Debug\Exception\FatalThrowableError :
Class 'RenameAuthorIdInPostTable' not found
Just try this code after deleting your existing migration for updating name,
php artisan make:migration rename_author_id_in_posts_table --table=posts
It will create one migration and then replace up() and down() functions with this in that created migration,
public function up()
{
Schema::table('posts', function (Blueprint $table) {
$table->renameColumn('author_ID', 'user_id');
});
}
And down() function with this,
public function down()
{
Schema::table('posts', function (Blueprint $table) {
$table->renameColumn('user_id', 'author_ID');
});
}
For more information about migration, you can go through this link Laravel Migrations.
PROBLEM WITH YOUR MIGRATION:
You are using Schema::create() method for change in table, As you have already created migration for posts table,You don't need to do Schema::create() just use Schema::table() method to update and change in any fields.
Schema::create('posts', function (Blueprint $table) {
$table->renameColumn('author_ID', 'user_id');
});
The error comes from the fact that your class name doesn't correspond to the name of the PHP migration file. Filenames are used to determine the name of the class they contain, so it is important to not rename a class or a file without renaming the other.
Rename your class UpdateUsernameInPostTable to RenameAuthorIdInPostTable and it should work.
If it does not, run composer dumpauto to reload the autoloading file and it will definitely work.
By following the below steps I can be able to rename the column name in Laravel 8
Step 1: Enter the below command to create a new migration file
php artisan make:migration update_author_ID_column --table=posts
Step 2: Write the below code in the created file. Follow the below code for up() function
public function up()
{
Schema::table('employee', function (Blueprint $table) {
$table->renameColumn('author_ID', 'user_id');
});
}
Step 3: Write the Below Code in down() function
public function down()
{
Schema::table('employee', function (Blueprint $table) {
$table->renameColumn('user_id', 'author_ID');
});
}
Step 4: Before running Migration command install the doctrine/dbal dependency
composer require doctrine/dbal
Step 5: finally run migration command.
php artisan migrate
you can use DB statement if you don't want to use doctrine
public function up()
{
DB::statement('ALTER TABLE posts CHANGE author_ID user_id bigint(20) ');
}
Best way would be to check if we have some column in our table or not, just avoid error while migrating;)
public function up()
{
if (Schema::hasColumn('table_name', 'old_col_name'))
{
Schema::table('table_name', function (Blueprint $table)
{
$table->renameColumn('old_col_name', 'new_col_name');
});
}
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
if (Schema::hasColumn('table_name', 'new_col_name'))
{
Schema::table('table_name', function (Blueprint $table)
{
$table->renameColumn('new_col_name', 'old_col_name');
});
}
}

How can I add field using alter table on the migration laravel?

I use laravel 5.3
My migration like this :
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration
{
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('api_token')->nullable();
$table->string('email',100)->unique();
$table->string('password')->nullable();
$table->string('avatar',100)->nullable();
$table->string('full_name',100)->nullable();
$table->date('birth_date')->nullable();
$table->smallInteger('gender')->nullable();
$table->timestamps();
$table->softDeletes();
});
}
public function down()
{
DB::statement('SET FOREIGN_KEY_CHECKS = 0');
Schema::dropIfExists('users');
DB::statement('SET FOREIGN_KEY_CHECKS = 1');
}
}
I want to add new field like this :
$table->string('mobile_number',20)->nullable();
But I don't want to add it on the schema. I want to use alter table
On my staging server and live server had set automatic migration
So if I use alter table, it will automatic migration. So on the table in database will automatic add mobile_number field if the code merge to development or master
If I add on the schema, it will not automatically migrate
How can I add the field using alter table?
You can use the schema to update an existing table. Create a new migration using the artisan command then add something like
Schema::table('users', function (Blueprint $table) {
$table->string('mobile_number',20)->nullable();
});
If you really wanted to do RAW sql you can do something like
DB::statement("ALTER TABLE users .....");
However the schema way is much better if you can get it to work
In the command line, execute artisan command to add new migration for your table:
php artisan make:migration add_mobile_number_to_users_table --table=users
Then you can put your code inside the newly created migration file:
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->string('mobile_number',20)->nullable();
}
}
public function down()
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('mobile_number');
}
}

Failing to create new migration

I want to create a new table, users_profile
here is the migration code
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersProfileTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('users_profile', function (Blueprint $table) {
$table->increments('id');
$table->string('city');
$table->string('country');
$table->integer('users_id')->unsigned();
$table->foreign('users_id')
->references('id')
->on('users')
->onDelete('cascade');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('users_profile');
}
}
facing an error
base table or view already exists: 1050 Table "users" already exists
here is the migration code of users
<?php
use Illuminate\Support\Facades\Schema;
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->string('first_name');
$table->string('last_name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
Although i am creating a new migration, a new table only i am declaring a primary key of users table as foreign key in users_profiles but facing the above error
help please!
Just a word of advice, Use Laravel Nomenclature to avoid these kind of issues. Always define your table names as plural snakecase and use singular camelcase model to access them.
In your case, change your table name to: user_profiles and use UserProfile model to access it automatically.
Ofocurse you need to change your class name in your migration accordingly: CreateUserProfilesTable
I believe it should resolve your issue.
I believe you are using : php artisan migrate and as users table already exists , running migration for users table again gives that error.
Use: composer dump-autoload and then php artisan migrate:refresh
to rollback all of your migrations and reinstall migrations.
Simple go to your database and drop Schema table ("users") and also remove entry in Schema table ("migrations") and run your migrations again...your problem is solve....

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