-
Notifications
You must be signed in to change notification settings - Fork 11.2k
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
[9.x] Option to set UUID/ULID as primary key on migration and model files #45350
Conversation
return 'ulid(\'id\')'; | ||
} | ||
|
||
return 'id()'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not just utilise Illuminate\Database\Schema\Blueprint::$defaultMorphKeyType
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks @crynobone , I will look into utilising that instead
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@crynobone, how do you suggest the Blueprint::$defaultMorphKeyType
be used in this section? I used the Blueprint
class to simplify all other approaches I initially did (thanks to you!), but I'm out of idea on how to make use of the said class in the line you commented on.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe it should 100% rely on Blueprint::$defaultMorphKeyType
without adding new --primary
or --key
options.
@@ -18,7 +19,8 @@ class MigrateMakeCommand extends BaseCommand | |||
{--table= : The table to migrate} | |||
{--path= : The location where the migration file should be created} | |||
{--realpath : Indicate any provided migration file paths are pre-resolved absolute paths} | |||
{--fullpath : Output the full path of the migration}'; | |||
{--fullpath : Output the full path of the migration} | |||
{--P|primary=int : Set default primary key type for the table <info>[int, uuid, ulid]</info>}'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
--key
can also be used and shorter but I don't find it as descriptive as --primary
. Thoughts?
@@ -72,6 +74,8 @@ public function handle() | |||
|
|||
$create = $this->input->getOption('create') ?: false; | |||
|
|||
Builder::defaultMorphKeyType($this->option('primary') ?: 'int'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, the command shouldn't change application defaults. The command should only use the default settings
return 'ulid(\'id\')'; | ||
} | ||
|
||
return 'id()'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe it should 100% rely on Blueprint::$defaultMorphKeyType
without adding new --primary
or --key
options.
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. |
Since #44146 we now have options to use UUID and ULID as the table's primary key. This PR aims to assist developers in setting up UUID or ULID during migration and model generation, following the instructions of the official documentation.
How to use:
Use the option
--primary
(or shortcut-P
) to define the type of primary key of the migration file or model being created.For example:
To generate a migration file that creates the table


books
with primary key typeUUID
:php artisan make:migration create_books_table -Puuid
To generate a



Customer
model that also creates the migration file with primary key typeULID
:php artisan make:model Customer -mPulid
More examples
Student
with-a
(all) options, and set the primary key to UUID:php artisan make:model Student --all --primary=uuid
Changes in this PR will not affect those who wish to use the default auto-incrementing integer as the primary key, as simply omitting the
primary
option (or leaving it empty) will result in the default PK.