Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.x] Disable autoincrement for unsupported column type #48501

Merged
merged 1 commit into from Sep 22, 2023

Conversation

ikari7789
Copy link
Contributor

ref: https://www.doctrine-project.org/projects/doctrine-dbal/en/3.6/reference/schema-representation.html

If the new column type is not of the correct data type, disable autoincrement.

Currently, if an autoincrement column is converted to a data type that does not support autoincrement, the flag remains true, causing the primary key to not be properly registered by the DBAL schema comparator.

Not sure if this bug fix constitutes as a backwards-breaking change or not.

The following migration demonstrates how this currently affects the generated table schema. You can hack around the issue by supplying a secondary change before the column type change that would remove autoincrement, but this seems to be the wrong approach to take here.

<?php

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

return new class () extends Migration {
    /**
     * Run the migrations.
     */
    public function up()
    {
        Schema::create('table_a', function (Blueprint $table) {
            $table->increments('id');
        });

        dump(DB::select("SELECT sql FROM sqlite_schema WHERE name = 'table_a'"));
        // array:1 [
        //   0 => {#724
        //     +"sql": "CREATE TABLE "table_a" ("id" integer primary key autoincrement not null)"
        //   }
        // ]

        Schema::table('table_a', function (Blueprint $table) {
            $table->binary('id')->change();
        });

        dump(DB::select("SELECT sql FROM sqlite_schema WHERE name = 'table_a'"));
        // array:1 [
        //   0 => {#722
        //     +"sql": "CREATE TABLE table_a (id BLOB NOT NULL)"
        //   }
        // ]

        Schema::create('table_b', function (Blueprint $table) {
            $table->increments('id');
        });

        dump(DB::select("SELECT sql FROM sqlite_schema WHERE name = 'table_b'"));
        // array:1 [
        //   0 => {#768
        //     +"sql": "CREATE TABLE "table_b" ("id" integer primary key autoincrement not null)"
        //   }
        // ]

        Schema::table('table_b', function (Blueprint $table) {
            $table->integer('id')->unsigned()->change();
            $table->binary('id')->change();
        });

        dump(DB::select("SELECT sql FROM sqlite_schema WHERE name = 'table_b'"));
        // array:1 [
        //   0 => {#777
        //     +"sql": "CREATE TABLE table_b (id BLOB NOT NULL, PRIMARY KEY(id))"
        //   }
        // ]
    }
};

ref: https://www.doctrine-project.org/projects/doctrine-dbal/en/3.6/reference/schema-representation.html

Autoincrement is only supported on smallint, integer, and bigint column
types. If a primary index column type is changed to an invalid type,
generated SQL will be missing the primary key declaration syntax.
@taylorotwell taylorotwell merged commit 165252b into laravel:10.x Sep 22, 2023
21 checks passed
@ikari7789 ikari7789 deleted the patch-1 branch September 25, 2023 00:22
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.

None yet

2 participants