Unexpected value of type Int: 26 of org.jetbrains.exposed.dao.EntityID #501

This commit is contained in:
Tapac
2020-01-29 22:34:30 +03:00
parent cfd14c5b86
commit efa78df1ea
2 changed files with 22 additions and 4 deletions

View File

@@ -336,9 +336,13 @@ open class Table(name: String = ""): ColumnSet(), DdlAware {
})
fun <ID:Comparable<ID>> entityId(name: String, table: IdTable<ID>) : Column<EntityID<ID>> {
val originalColumn = (table.id.columnType as EntityIDColumnType<*>).idColumn
val originalColumn = (table.id.columnType as EntityIDColumnType<*>).idColumn as Column<ID>
return entityId(name, originalColumn)
}
fun <ID:Comparable<ID>> entityId(name: String, originalColumn: Column<ID>) : Column<EntityID<ID>> {
val columnTypeCopy = originalColumn.columnType.cloneAsBaseType()
val answer = Column<EntityID<ID>>(this, name, EntityIDColumnType(Column<ID>(table, name, columnTypeCopy)))
val answer = Column<EntityID<ID>>(this, name, EntityIDColumnType(Column<ID>(originalColumn.table, name, columnTypeCopy)))
_columns.addColumn(answer)
return answer
}
@@ -599,6 +603,13 @@ open class Table(name: String = ""): ColumnSet(), DdlAware {
onDelete: ReferenceOption? = null, onUpdate: ReferenceOption? = null): Column<EntityID<T>> =
entityId(name, foreign).references(foreign.id, onDelete, onUpdate)
@JvmName("referenceByIdColumn")
fun <T:Comparable<T>, E: EntityID<T>> reference(name: String, refColumn: Column<E>,
onDelete: ReferenceOption? = null, onUpdate: ReferenceOption? = null) : Column<E> {
val entityIDColumn = entityId(name, (refColumn.columnType as EntityIDColumnType<T>).idColumn) as Column<E>
return entityIDColumn.references(refColumn, onDelete, onUpdate)
}
fun <T:Comparable<T>> reference(name: String, refColumn: Column<T>,
onDelete: ReferenceOption? = null, onUpdate: ReferenceOption? = null): Column<T> {
val originalType = (refColumn.columnType as? EntityIDColumnType<*>)?.idColumn?.columnType ?: refColumn.columnType
@@ -615,6 +626,13 @@ open class Table(name: String = ""): ColumnSet(), DdlAware {
onDelete: ReferenceOption? = null, onUpdate: ReferenceOption? = null): Column<T?> =
Column<T>(this, name, refColumn.columnType.cloneAsBaseType()).references(refColumn, onDelete, onUpdate).nullable()
@JvmName("optReferenceByIdColumn")
fun <T:Comparable<T>, E: EntityID<T>> optReference(name: String, refColumn: Column<E>,
onDelete: ReferenceOption? = null, onUpdate: ReferenceOption? = null) : Column<E?> {
val entityIDColumn = entityId(name, (refColumn.columnType as EntityIDColumnType<T>).idColumn) as Column<E>
return entityIDColumn.references(refColumn, onDelete, onUpdate).nullable()
}
fun <T:Any> Column<T>.nullable(): Column<T?> {
val newColumn = Column<T?> (table, name, columnType)
newColumn.referee = referee

View File

@@ -179,14 +179,14 @@ class EntityTests: DatabaseTestsBase() {
}
object Posts : LongIdTable(name = "posts") {
val board = optReference("board", Boards)
val board = optReference("board", Boards.id)
val parent = optReference("parent", this)
val category = optReference("category", Categories.uniqueId).uniqueIndex()
val optCategory = optReference("optCategory", Categories.uniqueId)
}
object Categories : IntIdTable() {
val uniqueId = uuid("uniqueId").clientDefault { UUID.randomUUID() }.uniqueIndex()
val uniqueId = uuid("uniqueId").autoGenerate().uniqueIndex()
val title = varchar("title", 50)
}