-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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
Incorrect Sorting with UUID Data Type in MariaDB #51883
Comments
Thanks @Karem-sobhy. Pinging you @staudenmeir as you were the author of #50192. Seems UUID columns aren't compatible with Laravel? I think we'll need more extensive tests in our MariaDB test suite for this one. |
Thank you for reporting this issue! As Laravel is an open source project, we rely on the community to help us diagnose and fix issues as it is not possible to research and fix every issue reported to us via GitHub. If possible, please make a pull request fixing the issue you have described, along with corresponding tests. All pull requests are promptly reviewed by the Laravel team. Thank you! |
Interesting issue. I don't order by UUID columns in my apps and so haven't run into it.
Not a very constructive statement. The issue is not that Laravel supports a native database data type. When you don't need your UUIDs to be sortable or already use IMO, the best solution would be using Instead, I would say that this needs to be addressed by MariaDB users in their applications:
|
Thanks @staudenmeir. I do feel we need some documentation around this and would be grateful if anyone could provide a PR. @Karem-sobhy please see @staudenmeir's reply around this topic. |
@staudenmeir using the old char(36) may be a fix but using the orderedUuid with mariadb is just bad for indexing and performance of the insertion and also non informed users maybe don't know all this because the documentation says clearly the uuid will be ordered which is not true when using mariadb default uuids I can help with a pr but don't know what will be best action to do so can you help me to chose from:
|
I honestly don't know. We can't make the breaking change and having a |
Thanks for the PR @Karem-sobhy. IMO, the For Laravel 12, I think we should consider again to switch to UUID @driesvints Do you remember why #44210 got reverted in the end? |
Hey @staudenmeir. I guess it's mainly this remark from @tillkruss:
Seems v7 has a data leak that's unwanted? |
Thanks @driesvints. I was thinking about "only" switching the When upgrading to Laravel 12, existing users of the |
@staudenmeir thanks for explaining. Right now I don't have the time to dig deeper into this matter. The best way forward is that you attempt a PR with whatever you want to try so Taylor can review it. Thanks 👍 |
@staudenmeir exactly what I was thinking about when creating the PR we leave |
@staudenmeir @Karem-sobhy I just saw PR #52029 with the new I mean this test is failing on MySQL / MariaDB: public function testUuidOrder()
{
Schema::create('orders', function (Blueprint $table) {
$table->id();
$table->uuid();
});
// we were using orderedUuid
DB::table('orders')->insert(['uuid' => (string) Str::orderedUuid()]);
// now switching to uuid7
DB::table('orders')->insert(['uuid' => (string) Str::uuid7()]);
// trying to get records in order, the actual value is [2, 1] and the test fails!
$this->assertEquals([1, 2], DB::table('orders')->orderBy('uuid')->pluck('id')->toArray());
} What am I missing here? |
@hafezdivandari The new trait can only be used for models that don't have any records yet. You can't switch to it. |
@staudenmeir thank you for explaining, I was so confused. |
@hafezdivandari as @staudenmeir said |
Thanks @Karem-sobhy, actually I was looking for a better solution to be used here on upcoming Passport 13.x. However this model's sortability doesn't rely on it's ID and I think we can use new uuid7 instead! |
@hafezdivandari If this approach is applied to Passport, it won't significantly impact current users because the structure remains the same, and it is not used for ordering. However, new Passport users will benefit from optimized order insertion and the ability to sort by ID when using MariaDB. |
I created a PR for Laravel 12: #52433 |
Laravel Version
11.11.1
PHP Version
8.3.8
Database Driver & Version
MariaDB 11.4.2
Description
When using the new UUID data type in MariaDB, the sorting is incorrect due to byte swapping performed by MariaDB to optimize UUIDv1 storage and indexing.
Details:
Problem:
When Laravel uses Str::orderedUuid() to generate a new ID for a model and insert it, MariaDB sees the version as < 6 and inserts it swapped. Consequently, the UUID is not ordered and not index-friendly, even though the purpose of the ordered UUID function is to ensure order and End of the table insert (Index friendly insert).
Conclusion:
Steps To Reproduce
.env
DB_CONNECTION=mariadb
$table->uuid('id')->primary();
HasUuids
trait inside the modelThe text was updated successfully, but these errors were encountered: