vapor-community / mongo-driver

MongoDB driver for Fluent
28 stars 26 forks source link

Fluent siblings relationship count always returns zero #39

Closed valeriomazzeo closed 6 years ago

valeriomazzeo commented 6 years ago

Consider two entities A and B, with a many-to-many relationship.

let a = A(id: 9)

let b1 = B(id: 1)
let b2 = B(id: 2)

try a.many_b.add(b1)

try a.many_b.all() -> returns 1 entity
try a.many_b.count() -> returns 0
try a.many_b.makeQuery().count() -> returns 0

From MongoDriver.swift line 241:

...
return try collection.count(filter, limitedTo: limit, skipping: skip).makeNode()
...

I can see that when calling count():

It seems to me that the filter is applied to the B table, which obviously doesn't contain a__id which belongs only to the pivot table Pivot<A, B>.

Not sure this is an issue in fluent or in mongo-driver, and I am also not sure if Fluent siblings are supposed to be used like this.

It could be related to https://github.com/vapor-community/mongo-driver/issues/38

/cc @tanner0101 @LoganWright @Joannis

FYI this works:

let aIdKey = Pivot<A, B>.leftIdKey
let total = try Pivot<A, B>.makeQuery().filter(aIdKey, 9).count()
print(total) -> 1

--- Edit:

I think the issue is that the count action implementation in MongoDriver doesn't honour the join that is in the query.

valeriomazzeo commented 6 years ago

fixed in https://github.com/vapor-community/mongo-driver/pull/48