1

I am working with laravel 5.6 and PostgreSQL 9.6 and I am trying to change the datatype of a column date from date to integer using a migration below.

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class ChangeBidDateTypeInAucBiddingsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('auc_biddings', function (Blueprint $table) {
            //
            $table->integer('bid_date')->change();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('auc_biddings', function (Blueprint $table) {
            //
            $table->date('bid_date')->change();
        });
    }
}

But every time I run the migration I get the following error

In PDOStatement.php line 107:

SQLSTATE[42804]: Datatype mismatch: 7 ERROR: column "bid_date" cannot be cast automatically to t ype integer HINT: You might need to specify "USING bid_date::integer".

Kindly assist on how I can achieve changing a column datatype using migration. Thanks

Below are some of the codes from composer.json

"require": {
        "php": ">=7.0.0",
        "doctrine/dbal": "^2.6",
        "fideloper/proxy": "~3.3",
        "laravel/framework": "5.5.*",
        "laravel/tinker": "~1.0",
        "predis/predis": "~1.0",
        "tymon/jwt-auth": "dev-develop"
    },
BAKARI SHEGHEMBE
  • 449
  • 6
  • 20
  • have you ran `composer require doctrine/dbal` ? – Sérgio Reis Mar 14 '18 at 14:06
  • Yes I have, I have edited the question to show my composer.json file – BAKARI SHEGHEMBE Mar 15 '18 at 10:10
  • Well, there are ways around it, like creating a new column and pass all data to there and delete the old, or use `DB::raw` and use direct alter tables to achieve that. but using what you're trying i'm afraid i can't help much more – Sérgio Reis Mar 15 '18 at 10:22
  • The solution in this and similar cases appears to be to code the SQL yourself. See also the answers to these other SO questions: https://stackoverflow.com/questions/22060398/how-do-i-change-a-column-type-with-laravel-schema-builder and https://stackoverflow.com/questions/63005845/postgres-and-laravel-how-to-change-column-from-type-string-to-integer – Sarah Messer Mar 14 '23 at 19:31

0 Answers0