vapor / fluent-kit

Swift ORM (queries, models, and relations) for NoSQL and SQL databases
MIT License
211 stars 116 forks source link

Can't query a single Field or the entire Group when querying a @Group modifier #608

Closed jcbriones closed 5 months ago

jcbriones commented 5 months ago

Describe the issue

Querying group fields not available for use.

Vapor version

4.94.0

Operating system and version

MacOS 14.4.1

Swift version

5.10.0

Steps to reproduce

Using the instructions from the documentation: https://docs.vapor.codes/fluent/model/#group

Should have the following class: User: Model and Pet: Fields

final class User: Model {
    // The user's nested pet.
    @Group(key: "pet")
    var pet: Pet
}

final class Pet: Fields {
    // The pet's name.
    @Field(key: "name")
    var name: String

    // The type of pet. 
    @Field(key: "type")
    var type: String

    // Creates a new, empty Pet.
    init() { }
}

Outcome

When I ran User.query(on: <Database>).field(\.$pet).all(). This does not work and gets a compile error because GroupProperty does not conform to QueryableProperty. The error would be something like Instance method 'field' requires that 'GroupProperty<User, Pet>' conform to 'QueryableProperty'

Also there is no way to only fetch single fields inside the group property like User.query(on: <Database>).field(\.$pet.$name).all() which should only get all names of the pets.

Additional notes

No response

gwynne commented 5 months ago

Okay, I just spent half an hour trying to make this work, and unfortunately it seems that the somewhat bizarre way that @Group was designed prevents this from being possible without breaking the API - or at least I couldn't find a way to do it 😕. If I missed something, I would gladly welcome a PR, but absent that - and given that development focus is now on Fluent 5 - I have to declare this (despite very much agreeing that this is a rather glaring omission) "not planned" for Fluent 4.

jcbriones commented 5 months ago

No worries. I just made it to be just part of the main model. Thank you for looking into it though. Maybe I can try play around with it and might be able to get a fix. But I didn't know there's fluent 5 coming up as well. That's nice. Is that like a redesign?