简体   繁体   中英

Laravel Migration deletes the record and the refresh it

I have created a table using migration and schema. I also added a field. then several records were inserted into the database. I understood that I forgot to add a column. I added a line in my Schema $table->string('activation', 256); and then in PowerShell I executed the following command to update the table:

php artisan migrate

it updated the table, but also deleted the records, what should I do to skip deletion of the records while updating the table. The following is the Schema:

Schema::create('users', function($table)
        {
                       $table->increments('id'); // incremental index
                       $table->string('username', 256); // username
                       $table->string('password', 256); // password
                       $table->string('name', 512); // full name of the user
                       $table->string('email', 512); // email address
                       $table->string('finding', 512); // the way the site found by the user
                       $table->string('mobile', 24); // the mobile number
                       $table->string('date', 256); // UNIX timestamp [saves in VARCHART to prevent any truncation
                       $table->string('birthday', 64); // birthday saved in dd-mm-yyyy format
                       $table->string('sex', 24); // saved in Persian characters male or female
                       $table->string('activation_code', 256);
                       $table->integer('activation_status');                       
                       $table->integer('newsletter'); // Boolean :: 0 -> no 1-> yes
                       $table->integer('province'); // the id of the province (corresponds to table.provinces)
                       $table->integer('city'); // the id of the city (corresponds to table.city)                       
                       $table->timestamps();
        });

There is no flaw there. You're just using it incorrectly.

You need to create a table, you do:

php artisan migrate:make create_table_users

Then you create your schema:

public function up()
{
    Schema::create('users', function($table)
    {
        $table->increments('id'); // incremental index
        $table->string('username', 256); // username
        $table->string('password', 256);
        ...
        $table->timestamps();
    });
}

public function down()
{
    Schema::dropIfExists('users');
}

The the method up is to migrate and the method down is to rollback the migration, it will drop your table.

Then you do

php artisan migrate

If you do again:

php artisan migrate

It will not work, because that migration is already migrated, to succeed in a second migrate you must:

php artisan migrate:rollback

The rollback command will execute the down method, which will, in this case, DROP your table.

If you just need to add a new column to your table you have to:

php artisan migrate:make add_column_to_users_table

And add

public function up()
{
    Schema::table('users', function($table)
    {
        $table->string('activation', 256);
    });
}

public function down()
{
    Schema::table('users', function($table)
    {
        $table->dropColumn('activation');
    });
}

And then you`ll be able to run

php artisan migrate

Again.

If migrate somehow deleted your records or table is because you've ran the wrong command.

If you need to add/edit/delete something from a migration after you have run it you should add another migration file. Some additional documentation for Laravel 4: http://laravel.com/docs/schema http://laravel.com/docs/migrations

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM