Instance Propertygrdb 7.2.0GRDB

exists

A boolean SQL expression indicating whether this alias refers to some rows, or not.

SQLGenerationContext.swift:539
var exists: SQLExpression { get }

In the example below, we only fetch books that are not associated to any author:

struct Author: TableRecord, FetchableRecord { }
struct Book: TableRecord, FetchableRecord {
    static let author = belongsTo(Author.self)
}

try dbQueue.read { db in
    let authorAlias = TableAlias()
    let request = Book
        .joining(optional: Book.author.aliased(authorAlias))
        .filter(!authorAlias.exists)
    let books = try request.fetchAll(db)
}