where expression for FieldRelationshipToMany properties #408

Open
opened 2025-12-29 15:31:05 +01:00 by adam · 1 comment
Owner

Originally created by @itShouldWork on GitHub (Mar 23, 2023).

Hi! There is a problem with .where expressions in case you are trying to filter toMany Relationship properties. In my code example I want to fetch all Master's which contains Pet with name Spot.

Code example:

final class Master: CoreStoreObject {
    @Field.Relationship("pets")
    var pets: [Pet]
}
final class Pet: CoreStoreObject {
    @Field.Stored("name")
    var name: String = ""
    @Field.Relationship("master", inverse: \.$pets)
    var master: Master?
}

func fetch() {
    let itWorks = From<Pet>()
        .where((\.$master ~ \.$pets ~ \.$name).any() == "Spot") // It doesn't reproduce any compiler errors

    let itDoesntWork = From<Master>()
        .where((\.$pets ~ \.$name).any() == "Spot") // Compiler errors: 
    // Operator function '~' requires that '[Pet]' conform to 'FieldRelationshipToOneType'
    // Operator function '~' requires that 'FieldContainer<Pet>.Stored<String>' conform to 'ToManyRelationshipKeyPathStringConvertible'
}

Seems like a common case, so maybe I'm doing something wrong?

Originally created by @itShouldWork on GitHub (Mar 23, 2023). Hi! There is a problem with `.where` expressions in case you are trying to filter *toMany* `Relationship` properties. In my code example I want to fetch all `Master`'s which contains `Pet` with name *Spot*. Code example: ``` final class Master: CoreStoreObject { @Field.Relationship("pets") var pets: [Pet] } final class Pet: CoreStoreObject { @Field.Stored("name") var name: String = "" @Field.Relationship("master", inverse: \.$pets) var master: Master? } func fetch() { let itWorks = From<Pet>() .where((\.$master ~ \.$pets ~ \.$name).any() == "Spot") // It doesn't reproduce any compiler errors let itDoesntWork = From<Master>() .where((\.$pets ~ \.$name).any() == "Spot") // Compiler errors: // Operator function '~' requires that '[Pet]' conform to 'FieldRelationshipToOneType' // Operator function '~' requires that 'FieldContainer<Pet>.Stored<String>' conform to 'ToManyRelationshipKeyPathStringConvertible' } ``` Seems like a common case, so maybe I'm doing something wrong?
adam added the investigating label 2025-12-29 15:31:05 +01:00
Author
Owner

@JohnEstropia commented on GitHub (Mar 24, 2023):

Thanks, it looks like a missing overload for the ~ operator

@JohnEstropia commented on GitHub (Mar 24, 2023): Thanks, it looks like a missing overload for the `~` operator
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/CoreStore#408