1

I'm trying to change the data type (from string to boolean) of 5 columns in my database, but it's showing an error that i have no idea what is the meaning. My migration is this:

<?php

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

class ChangeFieldDataType extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->boolean('categoria_pneu')->change();
            $table->boolean('categoria_dicas')->change();
            $table->boolean('categoria_servicos')->change();
            $table->boolean('categoria_dicas_gerais')->change();
            $table->boolean('categoria_variados')->change();
        });
    }

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

The erros that's showing, is this: enter image description here

I don't know what is the meaning of the error, since i'm trying to change the type to BOOLEAN and not TINYINT (i don't actually know if it's the same...)

Tim Lewis
  • 27,813
  • 13
  • 73
  • 102
EduardoK
  • 124
  • 10
  • 1
    https://stackoverflow.com/questions/3751853/boolean-vs-tinyint1-for-boolean-values-in-mysql `boolean` and `tinyint` are the same (assuming this is MySQL from the error). Is the database empty when running this migration? Also, what datatype are the columns changing from? – Tim Lewis May 20 '22 at 19:26
  • Right.. it is MySQL.. No, the database is not empty, but i've done this before and it worked, i just wasn't changing it to boolean.. The datatype is string to boolean, i've said that in the question – EduardoK May 20 '22 at 19:28
  • 1
    Whoops, you did say that; apologies! So that data type likely isn't "convertible"; I'm not sure why the error is saying "Invalid datetime format", but I can understand the `TRUNCATED incorrect integer value ''`, `''` isn't a valid value for a `tinyint` column. You might need to add an extra column, write a script to map the old values to this new column/type, then a migration to drop the old column (or similar). Or, if the values don't need to map over, empty them first (`UPDATE column SET value = NULL` for all 5 columns, then run the migration) – Tim Lewis May 20 '22 at 19:32
  • 1
    Haha yeah, that one is definitely bizarre; kinda looks like a different issue leaking into that error message. Also, I think you mean "since we aren't in production still" (or "we are in development still"), i.e. this project is not live yet. But, if that is the case, you can also simply modify your old migration, change the types, then rollback and re-run all your migrations/seeders; whatever works. Happy to help, and good luck! – Tim Lewis May 20 '22 at 19:39
  • 1
    Yes, i meant that the project is not live yet, sorry about the confusion haha.. Yes!! it worked now!! the columns have changed their datatype fine!! Thanks man!! – EduardoK May 20 '22 at 19:41
  • You can't change any column type to any other column type, they need to be convertible to each other without data loss. – apokryfos May 20 '22 at 21:38

0 Answers0