Hi all! I started implementing Exposed in a projec...
# exposed
z
Hi all! I started implementing Exposed in a project, but I ran into two issues. I worked around them with raw
exec
statements for now, but wanted to share them to see if they might be common enough to make a PR for in Exposed. 1. Unable to create virtual columns that are populated using a function (MySQL's
VIRTUAL
keyword) 2. Unable to set the sort order of indexed columns. This is most applicable when you need mixed sort orders, such as
(column_a ASC, column_b DESC)
. Right now, it defaults to all columns sorted ASC.
c
Hi @Zachary Sistrunk If you're still looking for more feedback, upvotes, or discussions about either of these topics, you could also try opening tickets in the Exposed YouTrack for additional engagement. If you are referring to MySQL's generated columns, this is possible by using
Column.withDefinition()
, which takes any number/combo of plain SQL strings +/- Exposed functions/operators:
Copy code
object Employees : Table("employees") {
    val firstName = varchar("first_name", 128)
    val lastName = varchar("last_name", 128)
    val fullName = varchar("full_name", 256)
            .withDefinition(
                "GENERATED ALWAYS AS (",
                concat(firstName, stringLiteral(" "), lastName),
                ") VIRTUAL"
            )
            .databaseGenerated()
}
I usually recommend chaining
.databaseGenerated()
to ensure Exposed doesn't complain at any point if you omit this column from other statements. I can't find a related ticket for the indexed column sort order, but it seems like a promising use case that is missing on the Exposed end. If you feel up for trying to implement a solution, we are certainly open to contributions.
z
Oh, I somehow missed the
.withDefinition
function, thanks! That should work perfect for what I need there.
👍 1