Skip to content

Commit c729e33

Browse files
committed
Fix non-deterministic query ordering after PostgreSQL migration and strengthen PK lookup semantics
1 parent ce3b891 commit c729e33

File tree

4 files changed

+4
-2
lines changed

4 files changed

+4
-2
lines changed

.claude/rules/backend/repositories.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Guidelines for implementing DDD repositories in the backend, including structure
3232
14. Never do N+1 queries
3333
15. Don't register repositories in DI—SharedKernel registers them automatically
3434
16. Don't add DbSets to DbContext—RepositoryBase handles this automatically
35+
17. Add `.OrderBy(e => e.Id)` when caller depends on order (`[0]`, `.First()`, pagination)—ULIDs are chronological
3536

3637
## Examples
3738

application/account/Core/Features/Authentication/Domain/SessionRepository.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public sealed class SessionRepository(AccountDbContext accountDbContext, IServic
4444
{
4545
public async Task<Session?> GetByIdUnfilteredAsync(SessionId sessionId, CancellationToken cancellationToken)
4646
{
47-
return await DbSet.IgnoreQueryFilters().FirstOrDefaultAsync(s => s.Id == sessionId, cancellationToken);
47+
return await DbSet.IgnoreQueryFilters().SingleOrDefaultAsync(s => s.Id == sessionId, cancellationToken);
4848
}
4949

5050
/// <summary>

application/account/Core/Features/Tenants/Domain/TenantRepository.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,6 @@ public async Task<Tenant[]> GetByIdsAsync(TenantId[] ids, CancellationToken canc
4141
/// </summary>
4242
public async Task<Tenant?> GetByIdUnfilteredAsync(TenantId id, CancellationToken cancellationToken)
4343
{
44-
return await DbSet.IgnoreQueryFilters().FirstOrDefaultAsync(t => t.Id == id, cancellationToken);
44+
return await DbSet.IgnoreQueryFilters().SingleOrDefaultAsync(t => t.Id == id, cancellationToken);
4545
}
4646
}

application/account/Core/Features/Users/Domain/UserRepository.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ public async Task<User[]> GetUsersByEmailUnfilteredAsync(string email, Cancellat
256256
return await DbSet
257257
.IgnoreQueryFilters([QueryFilterNames.Tenant])
258258
.Where(u => u.Email == email.ToLowerInvariant())
259+
.OrderBy(u => u.Id)
259260
.ToArrayAsync(cancellationToken);
260261
}
261262

0 commit comments

Comments
 (0)