artisan migrate:rollback error in laravel 5.3 - php

I'm trying to build a simple migration table and then trying to add a column in the table so following is my migration file:
class AddFlagToEmiTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('epins', function (Blueprint $table) {
$table->boolean('flag');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('epins', function (Blueprint $table) {
//
});
}
}
In this I forgot to write the drop value which is:
$table->dropColumn('flag');
Now while adding this and trying to rollback and again the trying to do php artisan migrate it is showing error, and also it is showing output nothing to migrate respectively even I've added drop values in the migration.
I've already tried following:
php artisan optimize
php artisan clear-compiled
composer dump-autoload
It ain't helping me out please see the screenshot:
Help me out. Thanks!

its because artisan cannot find flag column in epins table
There is workaround for this,
add flagcolumn explicitly from phpmyadmin(if you are using mysql) and then try roll back
Hope this method works, ask if any doubt

Please try deleting the tables from the database, modify the migration files and add drop to the down function and migrate again.

Related

Error Users already exits when trying to create a column to an existing table

I'm trying to add a new column to an existing table 'users'. I created a new migration 'add_privilege_column_to_users_table'. When I try to run the php artisan migration, I get an error that the 'users' table already exists. Am I missing something here?
I've already tried several methods from Googling and from other people that have had the same problem here at starckoverflow. But none of the code from here helped me.
class AddPrivilegesColumnToUsersTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->string('privilege_type')->after('remember_token');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('users', function (Blueprint $table) {
});
}
}
PDOException::("SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' already exists")
That's the specific error that I get when I run php artisan migrate. I'm just trying to add a new column to that table.
Below is my migration status:
Run? | Migration
No 2014_10_12_000000_create_users_table
No 2014_10_12_100000_create_password_resets_table
No 2019_07_28_071643_add_privileges_column_to_user_table
It seems you rolled back too far, but your users table was not dropped thus Laravel, when trying to run migrations, is trying to re-create the table.
If you head over to your database, you'll be able to update the migration to say it has already executed thus skipping over that batch.
UPDATE migrations SET batch = 1 WHERE migration = '2014_10_12_000000_create_users_table';

Schema::dropIfExists() Deleted ALL tables in DB on migrate:rollback

I ran php artisan migrate:rollback on command line, so the migration deleted ALL TABLES in db.
There is only one migration created but have others tables in used db.
The laravel documentation says:
To drop an existing table, you may use the drop or dropIfExists methods.
and the function down() on migration code is the default like as bellow:
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('image')->unsigned()->nullable();
$table->string('name', 64);
$table->string('slug', 64)->unique();
$table->integer('price')->unsigned();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('products');
}
}
I don't think the Schema::dropIfExists('products') work properlly in this case, this is a bug? or my(and laravel documentation) mistake?
Roll back, rolls back each migration of the last batch.
If your all migration in same batch then it will rollback all the migrations. So your all tables will be deleted from the database.
I know this post is over a year old, but this might help others who are having this issue and are looking for a solution. I'm guessing what happened here is if you run php artisan migrate:rollback it will rollback all migrations from the last batch. In other words, it wil run all down() functions in all the migrations in the same batch. So if you have made a down() function in those migrations where you revert the changes (deleting the created table), those will be ran as well.
To prevent this, you can add the --step parameter to indicate how many migrations should be rolled back, like so: php artisan migrate:rollback --step=1. Then only your last migration will be rolled back.

After migrations application does't working (Base table or view not found)

It all started with the fact that i created new migration
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('a_delivery_pec_pickup_coef', function(Blueprint $table) {
$table->primary(['location_id', 'weight_limit'], 'location_id_weight_limit_index');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('a_delivery_pec_pickup_coef', function(Blueprint $table) {
$table->dropIndex(['location_id_weight_limit_index']);
});
}
After i executed command
php artisan migrate:refresh --seed
and got error
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'delivery_update.
a_delivery_pec_delivery_ranges' doesn't exist (SQL: select * from a_delive
ry_pec_delivery_ranges)
Then something very strange began. I could not execute any commands becouse got same error.
php artisan migrate:rollback
php artisan migrate
php artisan list
application just stopped working. Main page stopped opening. I go into connection.php file and did
error_reporting(E_ALL);
ini_set('display_errors', 1);
Main page show same error, but insead table "a_delivery_pec_delivery_ranges" i saw another table.
Next i created table "a_delivery_pec_delivery_ranges" manually and the error was gone. BUT when i executed
php artisan migrate
i got error
Base table or view already exists: 1050 Table 'a_delivery_
pec_delivery_ranges' already exists
Now I'm in a deadlock situation becouse if i remove table 'a_delivery_
pec_delivery_ranges' my application not working with first error.
But if i add this table (also i tried add migration name into migrations table) and try
php artisan migrate
or
php artisan migrate:rollback
i get 'a_delivery_pec_delivery_ranges' already exists! Also i tried comment "up"-method in this migration
public function up()
{
Schema::create('a_delivery_pec_delivery_ranges', function (Blueprint $table) {
$table->increments('id');
$table->integer('weight');
$table->float('volume');
$table->float('length');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('a_delivery_pec_delivery_ranges');
}
but then i can't create this table!
Also i can't understand why it is this table! I delete each table in turn and and nothing happens. But if i remove this table my application die!
up. Problem is is relevant
What's the class (and file) name of this migration?
Did you mabe use the same class name in another migration file? I had this error once because of copy&paste lazyness ^^
Can you show the last files that made it through the migration, like which seeding was executed and which one doesnt?
Sorry for making an answer, I am 6 points shy of being able to comment :(
The problem was that in my app/Console/Commands/MyCommand.php in __construct was next code
PecDeliveryRange::all();

How to add column in a table using laravel 5 migration without losing its data?

I have an existing database table and I want to add column on it. However, as I run the php artisan migrate command, it says nothing to migrate. But I already add a Schema for adding table columns. I have read some articles and links that I should run the php artisan migrate:refresh first before the new columns to be added.The problem is, it will erase my existing data in my table. Is there any way I could perform the migration and successfully add columns in my table without deleting my data? Please help me with this. Thanks a lot. Here is my migration code.
public function up()
{
//
Schema::create('purchase_orders', function(Blueprint $table){
$table->increments('id');
$table->string('po_code');
$table->text('purchase_orders');
$table->float('freight_charge');
$table->float('overall_total');
$table->timestamps();
});
Schema::table('purchase_orders', function(Blueprint $table){
$table->string('shipped_via');
$table->string('terms');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
//
Schema::drop('purchase_orders');
}
I want to add column shipped_via and terms in my purchase_orders table.
Use below command to modify the existing table
php artisan make:migration add_shipped_via_and_terms_colums_to_purchase_orders_table --table=purchase_orders
use --create for creating the new table and --table for modifying the existing table.
Now a new migration file will be created. Inside the up() function in this file add these line
Schema::table('purchase_orders', function(Blueprint $table){
$table->string('shipped_via');
$table->string('terms');
});
And then run php artisan migrate
Laravel has a table in your database where it keeps track of all the migrations that are already executed. So by only changing the migration file Laravel will not automatically rerun that migration for you. Cause the migration is already executed by Laravel.
So the best thing to do is to just create a new migration and put the piece of code in it you already have (you were on the right track!).
public function up()
{
//
Schema::table('purchase_orders', function(Blueprint $table){
$table->string('shipped_via');
$table->string('terms');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
//
}
You don't need to populate the down function case the table will be dropped by your current purchase_orders migration.
To migrate the new migration just run:
php artisan migrate

How to create two tables migration using php artisan command for Laravel Project?

I am using composer for migrating two tables like this:
php artisan make:migration create_two_tables --create="projects","tasks"
Its creating the file in database->migration folder.
but while migrating using
php artisan migrate
its creating only table in database like this:
'projects,tasks'
as one table.
I want only one file and only one command like what I do in the top for migrating two tables in db.
Is there any possibility to get this?
Note: My superior ordered me to not change the database migration file at any cost...
Can anyone help me out here...
The make:migration command is just to create a new migration file from a template. To actually define the migration you normally have to edit that file. So in your case you would do this:
php artisan make:migration create_two_tables --create="projects"
Then open the ***_create_two_tables.php migration file and add the second table:
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('projects', function(Blueprint $table)
{
});
Schema::create('tasks', function(Blueprint $table)
{
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('projects');
Schema::drop('tasks');
}
Usually you also want to add actual columns to your tables. And that you do inside the Schema::create closure:
Schema::create('projects', function(Blueprint $table)
{
$table->increments('id');
$table->string('name');
// and so on
});
Read more about creating tables with the Schema Builder here
If you want to just create two tables through artisan and not add any columns to those tables you can create two migrations. Create one for each table. When you run
php artisan make:migration create_projects_table --create=projects
php artisan make:migration create_tasks_table --create=tasks
php artisan migrate
it will execute both migrations and create both tables for you.

Categories