Skip to content

[11.x] Fix modifying auto-increment columns #49925

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

Merged
merged 9 commits into from
Jan 31, 2024

Conversation

hafezdivandari
Copy link
Contributor

@hafezdivandari hafezdivandari commented Jan 31, 2024

This PR fixes handling auto-increment columns on some edge cases:

Create a compound primary with an auto-increment column

All databases except SQLite support creating a table with a compound/composite primary key with an auto-increment column:

Schema::create('test', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->uuid('uuid');

    $table->primary(['id', 'uuid']);
});

Modifying an auto-increment column

All databases except SQL Server support modifying an auto-increment/primary column:

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

// Change
Schema::table('test', function (Blueprint $table) {
    $table->bigIncrements('id')->change();
});

Changing a column to auto-increment

MySQL, MariaDB and SQLite support changing a regular column to an auto-increment column, but you must explicitly include primary modifier for this to work:

// Create
Schema::create('test', function (Blueprint $table) {
    $table->unsignedBigInteger('id');
});

// Change
Schema::table('test', function (Blueprint $table) {
    $table->bigIncrements('id')->primary()->change();
});

Upgrade Guide

We should add a note to docs:

The change method does not change indexes of a column. Therefore, you may use index modifiers explicitly to add/drop an index when modifying the column:

$table->bigIncrements('id')->primary()->change();
$table->char('postal_code', 10)->unique(false)->change();

Copy link

Thanks for submitting a PR!

Note that draft PR's are not reviewed. If you would like a review, please mark your pull request as ready for review in the GitHub user interface.

Pull requests that are abandoned in draft may be closed due to inactivity.

@hafezdivandari hafezdivandari marked this pull request as ready for review January 31, 2024 14:46
@taylorotwell taylorotwell merged commit 4c81886 into laravel:master Jan 31, 2024
@taylorotwell
Copy link
Member

@hafezdivandari can you PR the upgrade guide? Thanks

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