Forbid whereSamePrimaryKey for null values (#1956)

This commit is contained in:
Simon Binder 2022-07-31 23:26:00 +02:00
parent c69b34278b
commit b2cb0f6000
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
1 changed files with 17 additions and 0 deletions

View File

@ -356,6 +356,13 @@ extension QueryTableExtensions<T extends Table, D>
/// Applies a [where] statement so that the row with the same primary key as
/// [d] will be matched.
///
/// Note that, as far as primary key equality is concerned, `NULL` values are
/// considered distinct from all values (including other `NULL`s).
/// This matches sqlite3's behavior of not counting duplicate `NULL`s as a
/// uniqueness constraint violation for primary keys, but makes it impossible
/// to find other rows with [whereSamePrimaryKey] if nullable primary keys are
/// used.
void whereSamePrimaryKey(Insertable<D> d) {
final source = _sourceTable;
assert(
@ -384,6 +391,16 @@ extension QueryTableExtensions<T extends Table, D>
return MapEntry(primaryKeyColumns[columnName]!, value);
});
assert(
primaryKeyValues.values
.every((value) => value is! Variable || value.value != null),
'Tried to find a row with a matching primary key that has a null value, '
'which is not supported. In sqlite3, `NULL` values in a primary key are '
'considered distinct from all other values (including other `NULL`s), so '
"drift can't find a matching row for this query. \n"
'For details, see https://github.com/simolus3/drift/issues/1956#issuecomment-1200502026',
);
Expression<bool>? predicate;
for (final entry in primaryKeyValues.entries) {
final comparison =