Skip to content

[11.x] Add support for non-primary auto-increment column #50155

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

Conversation

hafezdivandari
Copy link
Contributor

@hafezdivandari hafezdivandari commented Feb 20, 2024

In addition to #49925, all databases except SQLite support creating a table with a non-primary auto-increment column. This wasn't supported on Laravel for MySQL and MariaDB, as MySQL needs an auto-increment column to be defined as key (i.e have index), so after this PR you can:

Schema::create('test', function (Blueprint $table) {
    $table->uuid()->primary();
    $table->bigIncrements('code')->index();
});

// create table `test` (`uuid` char(36) not null, `code` bigint unsigned not null auto_increment, index `users_id_index` (`code`), primary key (`uuid`))

or using unique index like this:

Schema::create('test', function (Blueprint $table) {
    $table->uuid()->primary();
    $table->bigIncrements('code')->unique();
});

// create table `test` (`uuid` char(36) not null, `code` bigint unsigned not null auto_increment, primary key (`uuid`), unique `users_id_unique` (`code`))

PS 1: The order of compiling indexes is based on MySQL docs.
PS 2: No upgrade guide entry is needed for this as everything works as before.

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.

@taylorotwell
Copy link
Member

Thanks for your pull request to Laravel!

Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include.

If possible, please consider releasing your code as a package so that the community can still take advantage of your contributions!

If you feel absolutely certain that this code corrects a bug in the framework, please "@" mention me in a follow-up comment with further explanation so that GitHub will send me a notification of your response.

@hafezdivandari hafezdivandari deleted the master-non-primary-auto-increment branch February 20, 2024 19:09
@victorcesae
Copy link

victorcesae commented May 21, 2024

For does who are searching about this feature, i found an workaround:

  1. Create a migration for your table and in the schema add somenthing like
$table->id('solicitation_number');
  1. So create another migration to modify the table that will house the non-primary auto-increment column. Here's the code for the up method within that migration::
Schema::table('PUT YOUR TABLE NAME IN HERE', function (Blueprint $table) {

            $getPrimaryKey = DB::connection('PUT YOUR CONNECTION IN HERE')->table('INFORMATION_SCHEMA.TABLE_CONSTRAINTS', 'TC')
                ->join('INFORMATION_SCHEMA.KEY_COLUMN_USAGE', function (JoinClause $join) {
                    $join->on('TC.CONSTRAINT_NAME', '=', 'KEY_COLUMN_USAGE.CONSTRAINT_NAME')
                        ->where('TC.CONSTRAINT_TYPE', '=', 'PRIMARY KEY')
                        ->where('KEY_COLUMN_USAGE.table_name', '=', 'PUT YOUR TABLE NAME IN HERE');
                })
                ->selectRaw("KEY_COLUMN_USAGE.table_name as TABLENAME,column_name as PRIMARYKEYCOLUMN,TC.CONSTRAINT_NAME")
                ->orderByRaw("KEY_COLUMN_USAGE.TABLE_NAME, KEY_COLUMN_USAGE.ORDINAL_POSITION")
                ->first();
            if ($getPrimaryKey && $getPrimaryKey->CONSTRAINT_NAME) {
                $table->dropPrimary($getPrimaryKey->CONSTRAINT_NAME);
            }
            $table->uuid('id')->primary();
});
  1. And in my down method i put:
Schema::table('PUT YOUR TABLE NAME IN HERE', function (Blueprint $table) {
     //
});

So this sql query will help you to find the primaryKey name and drop the correct primarykey

@Baysen
Copy link

Baysen commented Nov 19, 2024

For anyone finding this: Just found out you can also achieve this by first creating the column as unique, then adding auto increment in a change...

Schema::table('table', function (Blueprint $table) {
     $table->unsignedBigInteger('int_id')
         ->unique()
});
Schema::table('table', function (Blueprint $table) {
     $table->unsignedBigInteger('int_id')
         ->unique()
         ->autoIncrement()
         ->change();
});

@pintend
Copy link
Contributor

pintend commented Jul 9, 2025

Another possible solution (works with singlestore)

Blueprint::macro('autoBigIncrementOnly', function (string $column) {
    /**
     * @var \Illuminate\Database\Schema\Blueprint $this
     */
    return $this->addColumn('autoBigIncrementOnly', $column)->index();
});

SingleStoreSchemaGrammar::macro('typeAutoBigIncrementOnly', function () {
    return 'BIGINT UNSIGNED AUTO_INCREMENT';
});
$table->autoBigIncrementOnly('id');

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.

5 participants