3

I'm trying to Alter my current table and can do it just fine with a SQL Query in my migration file like this:

<?php
    public function up()
    {
        Schema::table('xyz', function (Blueprint $table) {
        DB::statement('ALTER TABLE mydatabase.xyz CHANGE funding_needed funding_needed decimal(10, 2) NOT NULL DEFAULT 0.00 ');
        DB::statement('ALTER TABLE mydatabase.xyz CHANGE funding_received funding_received decimal(10, 2) NOT NULL DEFAULT 0.00 ');
        });
    }
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('xyz', function (Blueprint $table) {
        DB::statement('ALTER TABLE mydatabase.xyz CHANGE funding_needed funding_needed varchar(191) NOT NULL ');
        DB::statement('ALTER TABLE mydatabase.xyz CHANGE funding_received funding_received varchar(191) NOT NULL DEFAULT 0');
        });
    }

But if I do it the this way I get an error:

<?php
    public function up()
    {
        Schema::table('xyz', function (Blueprint $table) {
             $table->decimal('funding_needed', 10, 2)->nullable(false)->default(0.00)->change();
             $table->decimal('funding_received', 10, 2)->nullable(false)->default(0.00)->change();
        });
    }
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('xyz', function (Blueprint $table) {          
         $table->string('funding_needed', 191)->nullable(false)->default(false)->change();
         $table->string('funding_received', 191)->nullable(false)->default(0)->change();
        });
    }

ERROR:

Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CHARACTER SET utf8mb4 DEFAULT '0' NOT NULL COLLATE utf8mb4_unicode_ci, CHANGE ' at line 1 (SQL: ALTER TABLE xyz CHANGE funding_needed funding_needed NUMERIC(10, 2) CHARACTER SET utf8mb4 DEFAULT '0' NOT NULL COLLATE utf8mb4_unicode_ci, CHANGE funding_received funding_received NUMERIC(10, 2) CHARACTER SET utf8mb4 DEFAULT '0' NOT NULL COLLATE utf8mb4_unicode_ci)

I want to do it the 2nd way due to running this in production. I don't want to modify the production migration to match my production database.

7
  • Don't know about the specific error you're getting, but, I think using NOT NULL and DEFAULT together is redundant. If it has a default value, well is obviously not going to be null, right?
    – Erubiel
    Commented Feb 17, 2020 at 20:10
  • You are correct. I was only trying to match the SQL Query. If I remove the nullable(false) and only keep the defaults it still doesn't run correctly.
    – daugaard47
    Commented Feb 17, 2020 at 20:17
  • Have you tried passing the argument as a string? 0.00 might not be a valid number in php, "0.00"
    – Erubiel
    Commented Feb 17, 2020 at 20:22
  • Works as a sting, but not as a decimal. I'm trying to change it from a String to a Decimal
    – daugaard47
    Commented Feb 17, 2020 at 20:26
  • I meant, $table->decimal('funding_needed', 10, 2)->nullable(false)->default("0.00")->change();
    – Erubiel
    Commented Feb 17, 2020 at 20:27

2 Answers 2

3

As pointed on this Github thread for doctrine/dbal package, which is the package you install before you run this update migrations, there's a bug on changing column types.

Using ->charset(null) along with ->change() should serve as a workaround in the meantime, as pointed on the link.

5
  • I tried your method a few ways and even tried to simply change $table->string to $table->decimal I still get error. The full error is updated in my original question.
    – daugaard47
    Commented Feb 17, 2020 at 20:45
  • So, even if you run the change method by itself you get an error? what error do you get if you only run that part?
    – Erubiel
    Commented Feb 17, 2020 at 20:50
  • If I run $table->decimal('funding_needed', 10, 2)->change(); I get the above error in my question.
    – daugaard47
    Commented Feb 17, 2020 at 20:51
  • 1
    Here you go: github.com/doctrine/dbal/issues/3714#issuecomment-571998622
    – Erubiel
    Commented Feb 17, 2020 at 20:58
  • 2
    PWEESH!! ->charset(null) before change did the trick! Thank you for helping on this. Update your answer so I can mark it correct.
    – daugaard47
    Commented Feb 17, 2020 at 21:02
0

guys, I had trouble to converting string to decimal and as @echo said in the comments

->charset(null)->change(); did the trick!

Thanks, guys for saving my time!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.