Closed valeriomazzeo closed 6 years ago
Consider two entities A and B, with a many-to-many relationship.
A
B
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:
MongoDriver.swift
241
... return try collection.count(filter, limitedTo: limit, skipping: skip).makeNode() ...
I can see that when calling count():
count()
MongoKitten.Collection<mongodb://localhost:27017/mydb.b>
Query(aqt: MongoKitten.AQT.valEquals(key: "a__id", val: "9"))
nil
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>.
a__id
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.
siblings
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.
count
MongoDriver
fixed in https://github.com/vapor-community/mongo-driver/pull/48
Consider two entities
A
andB
, with a many-to-many relationship.From
MongoDriver.swift
line241
:I can see that when calling
count()
:MongoKitten.Collection<mongodb://localhost:27017/mydb.b>
Query(aqt: MongoKitten.AQT.valEquals(key: "a__id", val: "9"))
nil
nil
It seems to me that the filter is applied to the
B
table, which obviously doesn't containa__id
which belongs only to the pivot tablePivot<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:
--- Edit:
I think the issue is that the
count
action implementation inMongoDriver
doesn't honour the join that is in the query.