Skip to content

[13.x] Allow ->using(...) in ->change() migrations for PostgreSQL compatibility - #60901

Merged
taylorotwell merged 5 commits into
laravel:13.xfrom
shemgp:13.x
Jul 27, 2026
Merged

[13.x] Allow ->using(...) in ->change() migrations for PostgreSQL compatibility#60901
taylorotwell merged 5 commits into
laravel:13.xfrom
shemgp:13.x

Conversation

@shemgp

@shemgp shemgp commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

This enables the use of ->using(...) for the function ->change() in migrations so that they will work in PostgreSQL.

PostgreSQL can't automatically convert some data types to others when altering the data type of a column via ALTER TABLE .. ALTER COLUMN.
The USING keyword can be used to tell PostgreSQL how to covert the data.

This PR adds that capability.

I don't think the other supported databases have the need for USING so it's just ignored in them in this fix.

For reference on the discussion:

Here's an example of a migration that needs it: https://github.com/aureuserp/aureuserp/blob/master/plugins/webkul/support/database/migrations/2025_11_14_102615_alter_currency_rates_table.php
Without it, it errors out with:

❌ Failed to wipe database: SQLSTATE[42804]: Datatype mismatch: 7 ERROR:  column "name" cannot be cast automatically to type date
HINT:  You might need to specify "USING name::date". (Connection: pgsql, Host: 127.0.0.1, Port: 5432, Database: aerp, SQL: alter table "currency_rates" alter column "name" type date, alter column "name" set not null, alter column "name" drop default, alter column "name" drop identity if exists)
Please manually drop your database and create a new one before proceeding.

when running php artisan erp:install when reinstalling it.

Below is the code where it shows the ->using(...) keyword added to make it work in PostgreSQL too:

<?php

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

return new class extends Migration
{
    public function up()
    {
        Schema::table('currency_rates', function (Blueprint $table) {
            $table->date('name')->using('name::date')->change();
        });
    }

    public function down()
    {
        Schema::table('currency_rates', function (Blueprint $table) {
            $table->string('name')->using('name::string')->change();
        });
    }
};

The benefit is that other Laravel Web Apps, which may be build for perhaps MySQL or SQLite, can be used with PostgreSQL as long as they use Laravel's Database Classes with some compatiblity fixes like this PR provides.

@taylorotwell
taylorotwell merged commit 76505c8 into laravel:13.x Jul 27, 2026
53 checks passed
@GrahamCampbell GrahamCampbell changed the title Allow ->using(...) in ->change() migrations for PostgreSQL compatibility [13.x] Allow ->using(...) in ->change() migrations for PostgreSQL compatibility Jul 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants