Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove primary key and auto-increment in database using migration in laravel

I have table with primary key and auto-increment field, I want make new migration to drop primary key index and also remove the auto increment field. How can i achieve this.

I created new migration as

public function up()
{
    Schema::table('tbl_message_read_state', function (Blueprint $table) {

    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::table('tbl_message_read_state', function (Blueprint $table) {

        $table->dropPrimary('message_id');
        $table->unsignedInteger('message_id');
    });
}

It gave me error in command as [Illuminate\Database\QueryException]
SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'message _id' (SQL: alter table tbl_'message_read_state' add 'message_id' int unsigned not null)

Whats Wrong ?????

like image 618
Vishal B Avatar asked Jun 09 '17 06:06

Vishal B


People also ask

Does Laravel migration use auto increment when its not primary?

laravel migration use auto increment when its not primary 0 add column auto increments without primary key in Laravel migrations Hot Network Questions Is a weapon proficiency also a skill proficiency, or are the two things completely distinct from each other?

How to remove primary key and AUTO INCREMENT in SQL Server?

In the Key column, there is PRI and in the Extra column there is auto_increment. You need to remove both of them, using the ALTER DROP command discussed in the beginning − We have successfully removed primary key and auto increment above.

How does the Order of migrations work in Laravel?

Each migration filename contains a timestamp that allows Laravel to determine the order of the migrations: Laravel will use the name of the migration to attempt to guess the name of the table and whether or not the migration will be creating a new table.

What is up and down method in Laravel migration?

A migration class contains two methods: up and down. The up method is used to add new tables, columns, or indexes to your database, while the down method should reverse the operations performed by the up method. Within both of these methods, you may use the Laravel schema builder to expressively create and modify tables.


4 Answers

Blueprint class offers dropPrimary methods that allow you to remove primary key.

public function down()
{
    Schema::table('table', function (Blueprint $table) {
        $table->dropPrimary();
        $table->unsignedInteger('id'); // for removing auto increment

    });
}
like image 97
Rahul Avatar answered Oct 28 '22 22:10

Rahul


This did it for me:

Schema::table('table_name', function (Blueprint $table) {
    // Make AI field `id` unsigned otherwise SQL 
    // will throw error when you try to remove it
    $table->integer('id')->unsigned()->change();

    $table->dropColumn('id');

    // If there was a foreign on message_id, make sure to remove it
    $table->dropForeign('table_name_message_id_foreign');

    $table->dropPrimary('message_id');
    $table->dropColumn('message_id');
}
like image 5
Adam Avatar answered Oct 28 '22 23:10

Adam


The dropPrimary method only removed the primary key, not the column. You would have to do:

    /**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::table('tbl_message_read_state', function (Blueprint $table) {

        $table->dropPrimary('message_id');
        $table->dropColumn('message_id');
        $table->unsignedInteger('message_id');
    });
}

Or instead of dropping and re-creating the column, you can use change in Laravel 5.

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::table('tbl_message_read_state', function (Blueprint $table) {

        $table->dropPrimary('message_id');
        $table->integer('message_id')->unsigned()->change();
    });
}
like image 4
sturrockad Avatar answered Oct 29 '22 00:10

sturrockad


You can try this

$table->dropPrimary('id_primary');
like image 3
Vishal Varshney Avatar answered Oct 28 '22 23:10

Vishal Varshney