![]() ![]() You should have one called create_users_table. Some databases - like MySQL - have performance problems with foreign keys when your primary key is a string. ![]() ![]() This feature of MySQL UUID allows us to merge. Their uniqueness does not depend on a central registration. I'm answering my own question to share that in the hope that it'll be useful. MySQL UUID not only creates unique values across the table but also across different databases and servers. When generated according to the standard methods, UUIDs are, for practical purposes, unique. Please note that if you apply the UUID change to the existing User model, then you will need to update your migrations. 74 I've spent quite some time looking for a solution and came up with the following mysql function that generates a random UUID (i.e. Use Illuminate\Foundation\Auth\User as Authenticatable Use Illuminate\Database\Eloquent\Factories\HasFactory Returns a short universal identifier as a 64-bit unsigned integer (rather than a string-form 128-bit. Use Illuminate\Contracts\Auth\MustVerifyEmail UUID() does not work with statement-based replication. Now, to make a model UUID-based, simply add the trait to the model with: use TraitUuid like so: namespace App\Models * Tell laravel that the key type is a string, not an integer. * Laravel that the identifier does not auto increment (it's a string). * Override the getIncrementing() function to return false to tell I do the same with interfaces and abstract classes. I am using the Trait prefix on the class name because I find this useful when finding files by name in my IDE. Now we need to create a trait which we will give to your models, in order to "convert" them to being UUID-based for their IDs, rather than an auto incrementing number. StepsĬreate a folder inside app called Traits if you haven't already. This tutorial will show you how you can quickly add a UUID trait, which you can then add to your models, so that they are using a UUID for their primary key/identifier. When I try to migrate the migration file it gives an obvious error because ( I guess ) mySQL does not allow you to create foreign key constraint on CHAR. I prefer to use PostgreSQL with UUIDs for the primary key. The default Laravel setup is for you to be using auto-incrementing integer IDs against a MySQL setup. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |