Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix associations altered by another association #677

Merged
merged 6 commits into from
Jan 12, 2020

Conversation

groue
Copy link
Owner

@groue groue commented Jan 11, 2020

This pull request fixes some requests that involve an association altered by another association.

For example, the Author.translatedBooks association below is such an altered association:

extension Book {
    static let translator = belongsTo(Translator.self)
}
extension Author {
    static let books = hasMany(Book.self)
    
    // A book is translated if it is associated to a translator
    static let translatedBooks = books.joining(required: Book.translator)
}

Some requests that involve those altered associations used to erroneously drop the alteration.

  • Requests which use the altered association with the request(for:) method:

    // Fixed
    extension Author {
        var translatedBooks: QueryInterfaceRequest<Book> {
            request(for: Author.translatedBooks) // !
        }
    }
    let author: Author = ...
    let translatedBooks: [Book] = try author.translatedBooks.fetchAll(db)
  • Requests that eager load an hasManyThrough association based on the altered association:

    // Fixed
    extension Book {
        static let awards = hasMany(Award.self)
    }
    extension Author {
        static let awardsForTranslatedBooks = hasMany(
            Award.self,
            through: translatedBooks,            // !
            using: Book.awards)
    }
    struct AuthorInfo: Decodable, FetchableRecord {
        var author: Author
        var awardsForTranslatedBooks: [Award]
    }
    let authorInfos: [AuthorInfo] = try Author
        .including(all: Author.awardsForTranslatedBooks)
        .asRequest(of: AuthorInfo.self)
        .fetchAll(db)

@groue groue added the bug label Jan 11, 2020
@groue groue changed the title Fix associations filtered by another association Fix associations altered by another association Jan 11, 2020
@groue groue merged commit ddccbaa into development Jan 12, 2020
@groue groue deleted the dev/fixAssociationFiltering branch January 12, 2020 08:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant