-
Notifications
You must be signed in to change notification settings - Fork 696
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
Add support for "generated always" columns #1558
Comments
I would also like a form of virtual columns on defining table schema. My example use case would be object Object : Table() {
val id = integer("id") // Column<Int>
val endDate = date("end_date") // Column<LocalDate>
val endTime: LocalDateTime = generatedAs {
append("(", this.endDate, " + TIME '23:59:59:9999') AT TIME ZONE 'America/Los_Angeles'")
}
} |
Related to this issue, I would like to know if it is possible to tell Exposed to ignore all columns not included in an insert statement. CREATE TABLE usr
(
id SERIAL PRIMARY KEY,
nm text NOT NULL,
age int NOT NULL,
_hash bigint GENERATED ALWAYS AS (hash_record_extended((nm, age), 0)) STORED
); My Exposed table looks like this: object UsrTable: IntIdTable("usr", "id") {
val nm = text("nm")
val age = integer("age")
val hash = long("_hash").nullable()
} When I run this: val usr = Usr(nm ="jill", age = 22)
val saved = UsrTable.insert {
it[nm] = usr.nm
it[age] = usr.age
}.resultedValues!!.map {
Usr(
id = it[UsrTable.id].value,
nm = it[UsrTable.nm],
age = it[UsrTable.age],
hash = it[UsrTable.hash]
)
} I get the following error: Since the database sets the value of object UsrTable: IntIdTable("usr", "id") {
val nm = text("nm")
val age = integer("age")
val hash = long("_hash").defaultExpression(CustomFunction("hash_record_extended", LongColumnType(), nm, age)).nullable()
} This is the query it produces and the error message I see in the log:
I'm not so concerned with the second error I got because I don't see the need to set a default expression for the column since the database will do that for me. I would like to know if there is a way to tell Exposed to not include a column in an insert statement if I do not list it in my insert statement. |
@darmstrong1 Since version 0.44.0, a column can be flagged as generated by the database using val hash = long("_hash").nullable().databaseGenerated() The column definition of the Exposed table object could also be adjusted if needed using object UsrTable : IntIdTable("usr", "id") {
val nm = text("nm")
val age = integer("age")
val hash = long("_hash")
.nullable()
.databaseGenerated()
.withDefinition("GENERATED ALWAYS AS (hash_record_extended((nm, age), 0)) STORED")
} |
@bog-walk your suggestion works, but this definition sets a db default to the column. Currently SchemaUtils cant handle this, it always recognize it as "IncorrectDefault" and try to drop it with DROP DEFAULT which fails (only DROP EXPRESSION works with generated columns). SchemaUtils should handle generated column's "default" separately. |
Databases support "GENERATED ALWAYS" columns which are columns that can compute values at the query time.
Users can include the columns in indices, and so on.
It would be nice if there was a possibility to declare a column that contains an expression.
For instance:
Then there should be a way to:
a) Include the virtual column into a primary key. Then update statement builder should be able to build queries like
update cities set ... where lower(name)=lower(?)
b) include the virtual column into index definition (e.g. to create index when creating table via Exposed)
c) use the virtual expression in the search somehow. For instance, if there's an index on
lower(name)
, then it would be interesting to build search conditions likewhere lower(name)=lower(?)
when user passes input ingredients (e.g. an expression forname
)See
The text was updated successfully, but these errors were encountered: