vapor / fluent-kit

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

`Model` using both `@CompositeID` and `@Children` crashes on load #512

Closed stevapple closed 1 year ago

stevapple commented 2 years ago

Describe the bug

A Model using both CompositeID and Children will crash on load.

To Reproduce

final class ParentModel: Model {
  static var schema = "parents"

  final class Identifier: Fields, Hashable {
    @Field(key: "id1")
    var id1: String
    @Field(key: "id2")
    var id2: Int

    func hash(into hasher: inout Hasher) {
      hasher.combine(id1)
      hasher.combine(id2)
    }
    static func == (lhs: ParentModel.Identifier, rhs: ParentModel.Identifier) -> Bool {
      lhs.id1 == rhs.id1 && lhs.id2 == rhs.id2
    }
  }
  @CompositeID
  var id: Identifier?

  @Children(for: \.$parent)
  var children: [ChildModel]
}

final class ChildModel: Model {
  static var schema = "children"

  @ID(custom: "id")
  var id: String?

  @Parent(key: "parent")
  var parent: ParentModel
}

Use such model layout, and the app will crash on loading ParentModel:

Could not cast value of type 'FluentKit.CompositeIDProperty<Test.ParentModel, Test.ParentModel.Identifier>' (...) to 'FluentKit.IDProperty<Test.ParentModel, Test.ParentModel.Identifier>' (...).

Expected behavior

A child should be able to have a parent with composite ID, which, in the sample layout, specified by parent_id1,parent_id2 with reference to parents(id1,id2). In reverse, a parent with composite ID should also be able to retrieve its children.

Environment

Additional context

Add any other context about the problem here.

gwynne commented 1 year ago

This has since been solved by the addition of CompositeParent and CompositeChildren et al.