Base table or view not found during migration - laravel

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

Related

Laravel - Add two column into existing table using php artisan make:migration at once

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"

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

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

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.

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.

How to drop table in Laravel?

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

Resources