Francesco Di Sciascio
04/09/2025, 12:56 PMval idRecordInserted = MyTable.insertAndGetId {
it[id] = -1
it[code] = myDataPassedInBodyOfRequest.code
...
}
Notice that I cannot remove the ID parameter because I will obtain an exception 'MyTableObject.id is not in record set', where 'MyTableObject' is the object of type IdTableLong defined to map the table as follows:
object MyTableObject: IdTable<Long>("my_table_name"){
override val id = long("id").entityId()
val code= varchar("code", 255)
...
}
Is there a way to automatically recover the ID of the inserted record, or do I need to read the sequence current value inside an exec() fun invocation?
Thanks allLuis Arcos
04/09/2025, 1:28 PMautoIncrement
Luis Arcos
04/09/2025, 1:28 PMoverride val id = long("ID").autoIncrement().entityId()
Luis Arcos
04/09/2025, 1:29 PMit[id] = -1
Francesco Di Sciascio
04/09/2025, 2:41 PMLuis Arcos
04/09/2025, 2:57 PMvalue
. Here is code we have:
internal fun insertAppointment(fields: AppointmentFields): AppointmentId {
return AppointmentTable.insertAndGetId {
it[advisorId] = fields.advisorId
it[studentId] = fields.studentId
it[start] = fields.appointmentTimeFrame.start
it[end] = fields.appointmentTimeFrame.endInclusive
it[institutionId] = fields.institutionId.value
it[departmentId] = fields.departmentId.value
it[appointmentTypeId] = fields.appointmentTypeId
it[createdAt] = fields.createdAt
it[createdBy] = fields.createdBy
it[appointmentReason] = fields.appointmentReason
it[location] = fields.meetingLocation.value
it[meetingMode] = fields.meetingLocation.type
it[appointmentRequestId] = fields.appointmentRequestId?.value
}.value
}
Francesco Di Sciascio
04/09/2025, 3:09 PMval id = StarWarsFilmsIntIdTable.insertAndGetId {
it[sequelId] = MOVIE_SEQUEL_ID
it[name] = "The Force Awakens"
it[director] = "J.J. Abrams"
}
It seems that value could be retrieved in this way, like I am doing without success.
The point is that the ID is created by sequence in database, and not by Exposed.
So, the question is: is there a way to retrieve that value without querying the database to get the current value of the sequence?Luis Arcos
04/09/2025, 6:05 PMFrancesco Di Sciascio
04/10/2025, 7:10 AMmy_table_name_id_seq
but I need to change it, like
seq_my_table_name
Francesco Di Sciascio
04/10/2025, 7:22 AMoverride val id = long("id").autoIncrement("sequence_name_ob_database").entityId()
Thanks for your help @Luis Arcos, I appreciate!