Laravel migration dropColumn drop the wrong column - php

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 ;)

Related

Why is Laravel 6.x creating a failed_jobs migration by default

I am creating a messages table in Laravel migrations but it's creating another table too called create_failed_jobs_table. I didn't create this, its a new project. It's happening in every project that I create it automatically creates this table too while creating also the other table, I don't know if its something I've done that creates it. Here is this file:
create_failed_jobs_table):
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateFailedJobsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('failed_jobs', function (Blueprint $table) {
$table->bigIncrements('id');
$table->text('connection');
$table->text('queue');
$table->longText('payload');
$table->longText('exception');
$table->timestamp('failed_at')->useCurrent();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('failed_jobs');
}
}
create_messages_table:)
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateMessagesTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('messages', function (Blueprint $table) {
$table->bigIncrements('id');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('messages');
}
}
To answer your question so that your question can be marked as answered: the failed_jobs table comes default with all Laravel 6.x projects. You can check the release note for other things that changed in release 6.0.
Notice that Laravel 6.0 also added a new driver option to the config. That's probably why they've also included the migration by default.

How to check database table which has been created in laravel 4.2?

I'm new in laravel, anyone know how to see database table which has been created? I've been migrate the database and stuck after create table using schema builder. This is my code
<?php
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($table)
{
$table->increments('id');
$table->string('nim');
$table->string('name');
$table->string('password');
$table->string('level');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
//
}
}
The new table name will be "users" because you have given 'users' as the name in the Schema::create function.
You can view the table from phpmyadmin if you are using the mysql database, which is sat as the default database for laravel.

Laravel Schema builder drop table on specific connection

I created a users table using Laravel's schema builder. Here is the migration:
class CreateUsersTable extends Migration {
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::connection('mysql-internal')->create('users', function(Blueprint $table)
{
$table->increments('id');
$table->string('username');
$table->string('password');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::connection('mysql-internal')->drop('users');
}
}
After running this migration, I then rolled it back using php artisan migrate:rollback. However, it looks like down method did not respect my connection. The users table was dropped from my default mysql database rather than the mysql-internal that I specified.
Did I do something wrong here? I am using Laravel 4.2.17.

Configuring oauth2-server-laravel with laravel-mongodb

I am trying to use oauth2-server-laravel with laravel-mongodb. After I generate migration using this command php artisan oauth2-server:migrations I tried to use php artisan migrate. But I got this error.
[ErrorException]
Missing argument 1 for Illuminate\Database\Schema\Blueprint::primary(),
called in
/home/opu/www/cwc_penguins/app/database/migrations/2015_01_19_203037
_create_oauth_scopes_table.php on line 17 and defined
2015_01_19_203037_create_oauth_scopes_table.php Migration code here
<?php
use Illuminate\Database\Schema\Blueprint;
use LucaDegasperi\OAuth2Server\Support\Migration;
class CreateOauthScopesTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
$this->schema()->create('oauth_scopes', function (Blueprint $table) {
$table->string('id', 40)->primary();
$table->string('description');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
$this->schema()->drop('oauth_scopes');
}
}
Remove this:
->primary()
And it should work.
Or you can change it to ->primary('id') (or whatever the field name is). That's what I did.

Laravel migrations error

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

Categories