luckyframework / luckyframework/website

Recommend setting default ordering when using UUID primary keys

Open
#548 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Crystal
Stars
39
Forks
62
PR merge metrics
No merged PRs in 30d

Description

PostgreSQL orders by primary key by default which makes complete sense when the primary key is an incrementing number. Calling `UserQuery.first` will always return the expected record in those cases. When the primary key is set to be a UUID, this becomes an issue. Without looking it up, I think it would then be ordered as you would sort strings so "A" comes before "B". Since UUIDs are random, the first UUID generated could start with any letter or number which means that the sorting could change over time. Calling `UserQuery.first` could return a different record tomorrow or the day after.

We should add documentation to set a default ordering in queries using UUID primary keys.

It would look like:

```crystal
class UserQuery < User::BaseQuery
def initialize
defaults &.created_at.asc_order
end
end
```

While Avram does provide a default ordering, it only does it if an ordering is not set
https://github.com/luckyframework/avram/blob/4f552db1c3769c741cffeb5233c91e93072b3c3e/src/avram/primary_key_queryable.cr#L22-L28

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the linked Avram primary_key_queryable.cr section and locating the relevant query documentation in the website. Explain why UUID primary keys need an explicit default ordering, include the UserQuery example from the issue, and make clear that Avram's default applies only when no ordering is set.

Written by the indexing model from the issue text.

Assessment

Tech stack
crystal, postgresql
Domain
databases, documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.