When I run php artisan migrate i get error B
Base table or view already exists: 1050 Table 'categories' already
exists'
What is it? Why? How to find error?
My categories migrations file:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
class CategoriesTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->string('title')->index();
$table->text('description');
$table->integer('attachment_id')->unsigned()->index();
$table->foreign('attachment_id')->references('id')->on('attachment')->onDelete('cascade');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
//
}
}
In that migration file, the function drop() should be
public function down()
{
Schema::drop('categories');
}
It seems that you try to create existing table..
so i think you are editing the migration file that you already installed
you can drop your database and re-migrate it
php artisan migrate:reset
then
php artisan migrate
Related
I just deployed on Heroku yesterday and connected to a Postgresql db and since then I have this fun error showing up on my screen (and terminal) on Heroku:
SQLSTATE[42P01]: Undefined table: 7 ERROR: relation "categories" does not exist LINE 1: select * from "categories" ^ (SQL: select * from "categories")
In my terminal, below this error, I have an Undefined table error stating that my categories table doesn't exist. This so frustrating because it does exist and it's right there! Can someone help with this? Has anyone had a similar Issue?
Tried:
Rollback tables: heroku run php artisan migrate:rollback
migrate fresh: heroku run php artisan migrate:fresh
migrate reset: heroku run php artisan migrate:reset
The migrations run up until the stories table where the relationship is located and then stop running. Right below stories is the categories table. I don't know how much this helps in fining a solution.
stories table:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateStoriesTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('stories', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('title');
$table->text('story');
$table->date('published_on');
$table->integer('count_views')->default(0);
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->unsignedBigInteger("category_id");
$table->foreign('category_id')->references('id')->on('categories')->ondDelete('cascade');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('stories');
}
}
categories table:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateCategoriesTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('category');
$table->string('title')->nullable();
$table->string('img')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('categories');
}
}
Story model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\Reviews;
use App\User;
use App\Category;
use App\ReadingList;
class Story extends Model
{
protected $guarded = [];
public function readingList()
{
return $this->belongsTo(ReadingList::class);
}
public function category()
{
return $this->belongsTo(Category::class);
}
public function reviews() {
return $this->hasMany(Reviews::class);
}
public function user() {
return $this->belongsTo(User::class);
}
}
Category model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\Story;
class Category extends Model
{
protected $guarded = [];
public function story()
{
return $this->hasMany(Story::class);
}
}
Been fixated on this for days, maybe you guys can see something I don't. Thank you so much in advance.
You should change date in migration file names
Example
2019_07_29_113341_create_categories_table
2019_07_30_113341_create_stories_table
the older date time would be the first to run,
you should migrate categories table then stories
Hope help you
That's because your categories table migration runs after stories table however stories table depends on categories because of the foreign keys. What you have to do is :
Rename the categories table migration and move it to top of the stories table migration
Drop all tables in the the DB
Run your migration again
you have to consider that first the Parent migration should be created and then the relationship should be established.
Create the "categories" migration first Then create the "stories" migration.
I hope it is useful..
You can create a single migration file in these scenarios.
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateStoriesTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('category');
$table->string('title')->nullable();
$table->string('img')->nullable();
$table->timestamps();
});
Schema::create('stories', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('title');
$table->text('story');
$table->date('published_on');
$table->integer('count_views')->default(0);
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->unsignedBigInteger("category_id");
$table->foreign('category_id')->references('id')->on('categories')->ondDelete('cascade');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('categories');
Schema::dropIfExists('stories');
}
}
I am working on a laravel project and each time I change my table (add or remove column) and run php artisan migrate:refresh. I get this error:
[Symfony\Component\Debug\Exception\FatalErrorException] Can't use
method return value in write context
Solution tried:
run composer dump-autoload (Fails)
Drop table in database, delete the migration file and restart again (works)
Previous migration file:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateCommentsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('comments', function (Blueprint $table) {
$table->increments('id');
$table->integer('post_id');
$table->string('body');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('comments');
}
}
Changed migration file:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateCommentsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('comments', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->integer('post_id');
$table->string('body');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('comments');
}
}
I added the user_id in the change file in the up function
Try this command it works for me
php artisan migrate:fresh
However, be careful! This command will drop all data from your DB:
Note: The migrate:fresh command will drop all tables from the database and then execute the migrate command.
as per Laravel docs 9.x.
try this
fire this command
php artisan make:migration add_user_id_to_comments_table --table=comments
this will create a new migration file then
$table->integer('user_id')->after('id');
then use
php artisan migrate
Refresh the database and run all database seeds...
php artisan migrate:fresh --seed
Read Docs
migrate generates the exception after pivot migration of Laravel 5.3.
schema1 : software_houses
schema2 : skill_domains
I generated the command make:migration:pivot skill_domains software_houses
and the migration is created by the name of skill_domain_software_house
but when I execute php artisan migrate
following error occurs :
[Symfony\Component\Debug\Exception\FatalThrowableError]
Class 'CreateSkillDomainSoftwareHousePivotTable' not found
See this Image
SKILL Domain
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateSkillDomainsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('skill_domains', function (Blueprint $table) {
$table->increments('id');
$table->string('name')->unique();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('skill_domains');
}
}
Software House
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateSoftwareHousesTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('software_houses', function (Blueprint $table) {
$table->increments('id');
$table->string('name',100)->unique();
$table->string('desc',500);
$table->string('url',100);
$table->string('rating')->default('3');
$table->string('logo')->nullable();
$table->string('city')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('software_houses');
}
}
Pivot table created after running the make:migration::pivot command
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateSkill_domainSoftware_housePivotTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('skill_domain_software_house', function (Blueprint $table) {
$table->integer('skill_domain_id')->unsigned()->index();
$table->foreign('skill_domain_id')->references('id')->on('skill_domains')->onDelete('cascade');
$table->integer('software_house_id')->unsigned()->index();
$table->foreign('software_house_id')->references('id')->on('software_houses')->onDelete('cascade');
$table->primary(['skill_domain_id', 'software_house_id']);
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('skill_domain_software_house');
}
}
Try with below class name line:
class SkillDomainsSoftwareHouses extends Migration
Make sure the file name should be match with class name Documentation.
UPDATE
I think you should try this:
first remove above software_houses,skill_domains from migrations folder also remove from migrations table in your database then follow below steps :
1) php artisan make:migration skilldomain
2) php artisan make:migration softwarehouses
3) php artisan make:migration:pivot skilldomains softwarehouses
4) php artisan migrate
I am having a problem in Laravel 5.3.
It won't allow me to drop a column from an existing table. I have run 'composer require doctrine/dbal' and that worked fine, but my column will not delete.
My add_column_to_table code:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddColumnToTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table)
{
$table->string('avatar')->default('default.pngs');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('users', function ($table)
{
$table->dropColumn('avatar');
});
}
}
Thanks
To drop the column you need to rollback the migration in the following manner
php artisan migrate:rollback
Try this:
public function down()
{
Schema::table('users', function (Blueprint $table)
{
$table->dropColumn('avatar');
});
}
}
You should rollback your migration:
php artisan migrate:rollback
But make sure that column is empty on your table.
I've a very weird issue i cannot explain. This make me worrying a lot because the project i'm working on is online and I might update the database columns sometimes.
// Migration file
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class ChangeUserProfilesTable extends Migration {
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('user_profiles', function($table)
{
// Keys
$table->datetime('status_updated_at')->nullable();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('user_profiles', function($table)
{
// Columns to remove
$table->dropColumn('status_updated_at');
});
}
}
Now I migrate, everything works well i checked and my table is the good one
html(master)$ php artisan migrate
Migrated: 2015_03_19_111236_change_user_profiles_table
Here's a part of my MySQL table
Now I rollback, as simple as it sounds.
html(master)$ php artisan migrate:rollback
Rolled back: 2015_03_19_111236_change_user_profiles_table
And now I just look at my table and it gets freaky
Yes, the status column vanished for no reason. And there's still the one that was supposed to be removed. I tried 10 times it just doesn't work ... I cannot even migrate anymore because
[Illuminate\Database\QueryException]
SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'status_
updated_at' (SQL: alter table `user_profiles` add `status_updated_at` datet
ime null)
Someone has an idea about it ? If it's an issue, it's a BIG one because migrations are really sensitive stuff within a project ... I don't really trust Laravel anymore and I wonder how I'll do with the production side.
**EDIT : To find a solution here are all the migrations linked with this table (but it shouldn't be called anyway ...)
I'm using Laravel 4.2**
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUserProfilesTable extends Migration {
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('user_profiles', function($table)
{
// Keys
$table->increments('id');
$table->integer('user_id')->unsigned()->nullable();
$table->integer('box_id')->unsigned()->nullable();
$table->string('stripe_customer');
$table->string('contract_id');
// Indexes
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('box_id')->references('id')->on('boxes')->onDelete('cascade');
// Timestamps
$table->timestamps();
$table->softDeletes();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('user_profiles', function(Blueprint $table)
{
$table->dropForeign('user_profiles_user_id_foreign');
$table->dropForeign('user_profiles_box_id_foreign');
});
Schema::dropIfExists('user_profiles');
}
}
And later
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class ChangeUserProfilesTable extends Migration {
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('user_profiles', function($table)
{
// Keys
$table->enum('status', array('not-subscribed', 'in-progress', 'subscribed', 'expired'));
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('user_profiles', function(Blueprint $table)
{
// Columns to remove
$table->dropColumn('status');
});
}
}
The fact I had the same name of classe in some migrations put composer in trouble. A composer dump-autoload allowed me to see it.
For instance :
Generating autoload files
Warning: Ambiguous class resolution, "ChangeBoxQuestionsTable" was found in both "/Users/Loschcode/Dropbox/htdocs/projets/bordeaux_in_box_lo/html/app/database/migrations/2015_03_12_183836_change_box_questions_table.php" and "/Users/Loschcode/Dropbox/htdocs/projets/bordeaux_in_box_lo/html/app/database/migrations/2015_03_19_040137_change_box_questions_table.php", the first will be used.
So I changed manually my files / classes names and also in the database migrations table too. Now it works fine ;)