1

I want to convert a field from string to boolean trying this code:

 public function up()
{
    Schema::table('users', function (Blueprint $table) {
        $table->boolean('email_permission')->change();
        $table->boolean('sms_permission')->change();
    });
}

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

but i got the error below

Doctrine\DBAL\Driver\PDOException::("SQLSTATE[42804]: Datatype mismatch: 7 ERROR:  column 
"email_permission" cannot be cast automatically to type boolean
HINT:  You might need to specify "USING email_permission::boolean".")

this is original migration:

   public function up()
{
    Schema::table('users', function (Blueprint $table) {
        $table->string('email_permission')->nullable()->default('0');
        $table->string('sms_permission')->nullable()->default('0');
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::table('users', function (Blueprint $table) {
        $table->dropColumn('email_permission');
        $table->dropColumn('sms_permission');
    });
}

how can i change this field type from integer to boolean ?

Afshn
  • 323
  • 1
  • 5
  • 16
  • please share the original migration – OMR Apr 04 '21 at 08:16
  • 2
    Is the DB type postgresql? Does this help? https://stackoverflow.com/questions/41149554/default-for-column-xxxx-cannot-be-cast-automatically-to-type-boolean-in-postgr – nice_dev Apr 04 '21 at 08:21

1 Answers1

0

i using this way and it work perfectly

 public function up()
{
    Schema::table('users', function (Blueprint $table) {
        DB::statement("ALTER TABLE users 
          ALTER COLUMN email_permission DROP DEFAULT,
          ALTER COLUMN email_permission TYPE BOOLEAN USING email_permission::BOOLEAN,
          ALTER COLUMN email_permission SET DEFAULT FALSE;");
    });
}
Afshn
  • 323
  • 1
  • 5
  • 16