Is It Possible To Change The Datatype Of A Column From Int To Double Without Losing Data With Laravel Migration
I'm trying to change the datatype one of my column. My migration to create table was:
public function up()
{
Schema::create('place_ratings', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('places_id');
$table->foreign('places_id')->references('id')->on('places');
$table->integer('rating');
$table->mediumText('comments');
$table->timestamps();
});
}
I want to change the datatype of rating table from integer
to float
. I already have some data in my table and I don't want to lose them. So, how can I do that?
Answer
You should be able to change that column using a simple migration, see https://laravel.com/docs/5.8/migrations#modifying-columns for additional info:
You need to install composer require doctrine/dbal
first for the ability to generate sql that changes these columns.
Schema::table('place_ratings', function (Blueprint $table) {
$table->decimal('rating', 16, 4)->change();
});
The 16
and 4
means the total length and the decimal precision, so in this case it would have numbers of range 0.0000
(nothing before the dot, 4 after) to 000000000000.0000
(12 before the dot, 4 after, so 16 total).
Be sure to run it locally first to make sure you don't have unwanted side effects.
Related Questions
- → "failed to open stream" error when executing "migrate:make"
- → October CMS Plugin Routes.php not registering
- → OctoberCMS Migrate Table
- → OctoberCMS Rain User plugin not working or redirecting
- → October CMS Custom Mail Layout
- → October CMS - How to correctly route
- → October CMS create a multi select Form field
- → October CMS - Conditionally Load a Different Page
- → How to disable assets combining on development in OctoberCMS
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → OctoberCms component: How to display all ID(items) instead of sorting only one ID?
- → In OctoberCMS how do you find the hint path?
- → How to register middlewares in OctoberCMS plugin?