Closed janigro closed 5 months ago
This is not a bug; Fluent model loading assumes a specific naming pattern of column aliases which your custom expression fails to follow. In your example, .field(.custom(SQLAlias(/* your actual SQLExpression here */, as: SQLIdentifier("\(FieldKey.prefix(.string(Role.schema), Role.path(for: \.$name)[0]))")))
would most closely approximate what Fluent actually does to generate column names.
That being said, this is not a supported way to use Fluent in the first place; dropping to SQLKit is the correct approach for this kind of need. (And even then, it's very strongly advised to avoid SQLRaw
as much as possible; it's generally only needed when expressing keywords for which no expression already exists, nearly everything else can be expressed by various combinations of SQLIdentifier
, SQLFunction
, SQLColumn
, SQLBind
, SQLLiteral
, SQLBinaryExpression
, SQLGroupExpression
, SQLList
, and so forth - I admit this tends to be exceptionally verbose, to be sure 😅).
Thanks for the superfast response! I'll keep using SQLKit then!
Btw, I'm not really using SQLRaw as my SQLExpression, I just use it for the simplicity in the example. ;-)
Describe the bug
Using
.field(.custom(SQLExpression))
to fetch data works fine, but crashes when trying to unwrap the value of the column.To Reproduce
The following code has been reduced to its minimal expression. My actual
SQLExpression
is much more complex than the one below. However, to demonstrate the problem, I chose the simplest expression possible:The example uses a table
roles
, with a columnname
:This fetches data correctly, as printing the row shows:
However, when trying to access the name column, the unwrap crashes the app:
Workaround
Alternatively, I use
vapor/sql-kit
, which works just fine using the sameSQLExpression
:Environment