Closed geeksweep closed 4 years ago
@tanner0101 @0xTim or whoever else. please hit me up on discord if you want me to run tests real time to help debug whats going on. I'll be around.
I confirm getting the same error with the suggested test code and a fully qualified KeyPath:
return Account.query(on: req).filter(\Account.paid == true).all() // Qualified KeyPath.
[ ERROR ] PostgreSQLError.server.error.errorMissingRTE: missing FROM-clause entry for table "Account" (Logger+LogError.swift:17)
But with partial keypaths:
return Account.query(on: req).filter(\.paid, .equal, true).all() // Partial KeyPath.
or:
return Account.query(on: req).filter(\.paid == true).all() // Partial KeyPath.
It works seamlessly.
WORKAROUND:
Set static var sqlTableIdentifierString
in the Account
struct :
struct Account: Content, Model, PostgreSQLTable {
typealias Database = PostgreSQLDatabase
typealias ID = Int
static var idKey : WritableKeyPath<Account, Int?> = \Account.accountid
static let entity = "account"
// ADD THIS PROPERTY TO RESOLVE ISSUE.
static var sqlTableIdentifierString = entity // Which is "account" (lower case).
var accountid:Int?
var info:[accountinfo]
var paid:Bool
}
allows to use the qualified form of the KeyPath.
PROBABLE CAUSE:
Apparently when using a fully qualified keypath in a predicate, the table name of a PostgreSQLTable
is derived from protocol SQLTable
defined in SQL 2.0.0-beta3/SQL/SQLTable.swift:
import Core
public protocol SQLTable: Codable, Reflectable {
static var sqlTableIdentifierString: String { get }
}
extension SQLTable {
public static var sqlTableIdentifierString: String {
return "\(Self.self)"
}
}
where the table name is set to the Type name. Hence the capitalized table name.
@mixio's workaround is correct. I think we will need to reconcile the difference between sqlTableIdentifierString
and entity
in the next version of this package. I can't think of a good way to do this in a non-breaking way.