-
-
Notifications
You must be signed in to change notification settings - Fork 116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
QueryBuilder.sum(KeyPath) always returns nil #379
Comments
I tried reproducing your issue, but it appears to work fine for me. import Fluent
import Vapor
final class Course: Model, Content {
static let schema: String = "courses"
@ID(key: .id)
var id: UUID?
@Field(key: "credits")
var credits: Int
}
struct CourseMigration: Migration {
func prepare(on database: Database) -> EventLoopFuture<Void> {
database.schema(Course.schema)
.id()
.field("credits", .int)
.create()
}
func revert(on database: Database) -> EventLoopFuture<Void> {
database.schema(Course.schema).delete()
}
} I then proceeded to apply the migration in app.databases.use(.sqlite(.file("db.sqlite")), as: .sqlite)
app.migrations.add(CourseMigration()) And for the func routes(_ app: Application) throws {
app.get { req in
return Course.query(on: req.db).filter(\.$credits < 100).sum(\.$credits) ?? -1
}
app.get("hello") { req -> EventLoopFuture<String> in
let c = Course()
c.credits = Int.random(in: 0 ..< 500)
return c.save(on: req.db).transform(to: "OK")
}
} Are you sure you ran the migrations with |
I use Postgres Database. Since my code with I have the following versions after
|
I've managed to reproduce your issue. It is, afaik, related to postgres and its built in SELECT column_name, data_type FROM information_schema.columns WHERE table_name = 'courses';
# column_name | data_type
# -------------+-----------
# id | uuid
# credits | bigint And to get the type of the returned sum value we can do the following. SELECT pg_typeof(sum) FROM (SELECT SUM(credits) from courses) as NESTED;
# pg_typeof
# -----------
# numeric The numeric result type is interpreted as
And you can now see that it translates perfectly fine to a |
I have a Class Course and try to use the
sum
method in the following way:As outlined in the documentation I would expect the query to return type Int.
However, I receive an Optional instead. Furthermore, the result of it always is nil. When I query with the code below, I receive the correct value.
The text was updated successfully, but these errors were encountered: