The problem is that I have this error:
[PDOException]
SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'songs' already exists
This is my migration file:
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateSongsTable extends Migration
{
public function up()
{
Schema::create('songs', function (Blueprint $table)
{
$table->increments('id');
$table->integer('user_id');
$table->string('title');
$table->string('slug')->unique();
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
}
}
I think the solution will be just to delete the table then run migration again, so how can I drop a table in Laravel 5 using the command line? I am using MySQL.
To drop a table, you may use the Schema::drop method:
Schema::drop('users');
// Better
Schema::dropIfExists('users');
To drop a table in laravel, Create a first migration
Step to drop a table
$ php artisan make:migration drop_user_table
Add this to your migrate file inside up function Schema::drop('tableName');
public function up()
{
Schema::dropIfExists(table('songs'));
$table->increments('id');
...
}
then run
$ php artisan migrate
You can use drop or dropIfExists methods:
Schema::drop('users');
Schema::dropIfExists('users');
You can also rollback if you want to drop your last migrated table
php artisan migrate:rollback
The migrate:reset command will roll back all of your application's migrations:
php artisan migrate:reset
The migrate:fresh command will drop all tables from the database and then execute the migrate command:
php artisan migrate:fresh
php artisan migrate:fresh --seed
You need a down method on your migration so that when you run php artisan migrate:rollback it can drop your database.
e.g.
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateSongsTable extends Migration
{
public function up()
{
Schema::create('songs', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->string('title');
$table->string('slug')->unique();
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
}
public function down()
{
Schema::drop('songs');
}
}
The simple way to Delete the table and run the Migration again. Just run the artisan command.
php artisan migrate:fresh
Note: It will drop all tables and re-run migration.
or if you have the seed of tables then run this command
php artisan migrate:fresh --seed
Reference: Laravel Documentation
For rollback of single table,
php artisan migrate:rollback --path=/database/migrations/22_03_18_010_create_users_table.php
For rollback and re-migrate at the same time,
php artisan migrate:refresh -path=\database\migrations\22_03_18_010_create_users_table.php
This command will save your time to drop and re-migrate a table after editing, often beginners drop the migration and then migrate it again after changes.
You can run php artisan db:wipe that way you are going to force laravel to drop all tables regardless foreign keys constraints or like in your case if you forgot the down() method on your migration.
if you don't have a down() method on your migration, rollback is not going to work.
You can also use php artisan migrate:fresh this is going to force drop all tables and then is going to run all migrations
Create a migration in the console with the following;-
php artisan make:migration drop_table_name_table;
Then you need edit the up() function in that migration to be something like;-
public function up()
{
Schema::drop('table_name');
}
Then run that migration to compelete the task.
Go to migration table and in down function add this line:
Schema::dropIfExists('tableName');
You will replace tableName with your table name in migration.
After adding it, open command line and remove this table with this command:
php artisan migrate:rollback --step=1 --path=database/migrations/2022_12_12_050004_create_tableName.php
Related
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
I am new to laravel and I want to know that can't we add two column into existing table using
php artisan make:migration
at once for Ex. if my users table contain id,user_name and now I want to add two new column like as user_phone and user_email in one
php artisan make:migration add_user_phone_to_users_table add_user_email_to_users_table
something like that ? I am very sorry If my question is wrong.. I can add new field one by one into two separate migration but want to know is it possible to add two new column to existing table at once. Thanks in advance and I hope I will get a satisfied answer.
You are right by creating a new migration, php artisan make:migration add_email_and_phone_number_to_users --table=users
In the migration you can add the code for this:
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->string('email')->nullable();
$table->string('phone_number')->nullable();
});
}
public function down()
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn(['email', 'phone_number']);
});
}
when adding more columns to an existing table using another migration, you need to define the table
php artisan make:migration name_of_the_migration --table="table_name"
Scenario: I create a column name price which have lots of data and now I need to change that 'price' Column name to oldPrice without losing it's data and using migration.
Is there any way please let me know. Thanks
To do or undo any changes in migrations. You must write new migration this will not let you or your team mates lose any data and your app will not break.
To make change in column use following :
Make a migration
php artisan make:migration change_price_to_old_price --table=table_name
and use below code according to your requirements.
In up() function:
public function up(){
Schema::table('table_name', function (Blueprint $table) {
$table->renameColumn('old_name', 'new_name');
});
}
In down() function just undo what you did in up()
public function down(){
Schema::table('table_name', function (Blueprint $table) {
$table->renameColumn('new_name', 'old_name');
});
}
Recommendation:
Always write down migration to undo what you are doing in your up function.
php artisan migrate:status to see which migrations have ran and which are remaining.
hit php artisan migrate: and see list of commands and learn about them
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.
I want to change my two table name in the Laravel, so do I have to manually change the table name or it can be possible through migration.
from the docs laravel.com/docs/5.1/migrations#renaming-and-dropping-tables
To change a table name, you can do this:
Schema::rename($currentTableName, $newTableName);
You can use the drop or dropIfExists methods to remove an existing table:
Schema::drop('users');
Schema::dropIfExists('users');
Just add that to a migration and it should work.
You can rename table like that
Schema::rename('old_table', 'new_table');
BUT be careful if you have foreign keys, indexes and unique-s.
you will not be able to deleted them after renaming, like thiat
Schema::table('new_table', function (Blueprint $table) {
$table->dropForeign(['transaction_id']);
});
because they will have old names and these names have table name in them.
Thus, I recommend deleting foreign keys and other stuff first
Schema::table('old_table', function (Blueprint $table) {
$table->dropForeign(['transaction_id']);
});
Schema::rename('old_table', 'new_table');
Schema::table('new_table', function (Blueprint $table) {
$table->foreign('transaction_id')->references('id')->on('transactions');
});
Firstly, use CLI command to create a migration:
php artisan make:migration rename_table
Now, in the up method of the new migration class, use the rename method to change table name:
Schema::rename('old_table_name', 'new_table_name');
Next, execute the migration command:
php artisan migrate
To rename an existing database table, use the rename method:
Schema::rename($from, $to);
To drop an existing table, you may use the drop or dropIfExists methods:
Schema::drop('users');
Schema::dropIfExists('users');
Firstly, run this in your terminal to create a migration file to rename a table:
php artisan make:migration rename_old_name_to_new_name_table
Then in the up method, you should have this:
public function up()
{
Schema::rename('old_table_name', 'new_table_name');
}
Then in the down method, you should have this in case you want to revert previous changes made:
public function down()
{
Schema::rename('new_table_name', 'old_table_name');
}