Skip to content

Commit 828af4c

Browse files
committed
Add the coalescing solution to caveats doc
1 parent f91a76b commit 828af4c

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

docs/caveats.md

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,19 @@ NULL is a very special value in databases. You can't compare against it as it in
66

77
This is why having a nullable column as part of the keyset _is not supported_ in this library, as we can't predictably deal with its special handling in databases.
88

9-
### Workaround
9+
### Solutions
10+
11+
There are two solutions for this:
12+
1. Computed column: Reliable, performant, a bit harder to implement
13+
2. Coalescing in keyset: Extremely easy to implement, but might suffer in terms of performance
14+
15+
#### Solution #1 - Computed column
1016

1117
The recommended way of working around this when you naturally have a nullable column that you want as part of your keyset is through using a non-nullable computed column (one for each nullable column in your keyset) and using that in your keyset instead.
1218

1319
You'll have to do this with any kind of nullable column type, but let's look at an example for a DateTime column.
1420

15-
Let's assume we want to create a keyset comprising of two columns: Created + Id. Let's assume that we need to make Created nullable for a business requirement.
21+
Let's assume we want to create a keyset comprising of two columns: `Created` + `Id`. Let's assume that we need to make `Created` nullable for a business requirement.
1622

1723
First, we'll need to introduce a property in our model that we'll configure as computed along our nullable property:
1824

@@ -37,7 +43,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
3743
modelBuilder.Entity<User>()
3844
.Property(x => x.CreatedComputed)
3945
// We're coalescing NULLs into a max date.
40-
// This results in NULLs effectively being sorted last (if ASC), irrelevant of the Db.
46+
// This results in NULLs effectively being sorted last (if ASC), irrelevant of the database.
4147
// You're writing sql here, make sure you have the right format for your particular database.
4248
// This is for sqlite.
4349
.HasComputedColumnSql("COALESCE(Created, '9999-12-31 00:00:00')");
@@ -69,3 +75,20 @@ _dbContext.Users.KeysetPaginateQuery(
6975
```
7076

7177
Check this [sample page](../samples/Basic/Pages/Computed.cshtml) for a working example you can run and play with.
78+
79+
#### Solution #2 - Coalescing in keyset
80+
81+
This is an extremely easy solution to the NULL problem. Let's assume again we have the following keyset: `Created` + `Id`, where `Created` is nullable.
82+
83+
Here's how to solve this:
84+
85+
```cs
86+
_dbContext.Users.KeysetPaginateQuery(
87+
b => b.Ascending(x => x.Created ?? DateTime.MinValue).Ascending(x => x.Id),
88+
KeysetPaginationDirection.Forward,
89+
reference)
90+
```
91+
92+
As you can see, even though `Created` itself is nullable, the keyset above uses a null coalescing to specify the default value when it's null.
93+
94+
In terms of the end result, this is equivalent to creating a computed column.

0 commit comments

Comments
 (0)