Christiano
01/03/2024, 12:45 PM(CategoriesTable innerJoin CategoriesTable.alias("parent")).selectAll()
And then tried to map the ResultRows into an object like this:
fun ResultRow.toCategory(): Category = Category(
id = this[CategoriesTable.id].value,
name = this[CategoriesTable.name],
category = this.toCategory(),
createdAt = this[CategoriesTable.createdAt],
updatedAt = this[CategoriesTable.updatedAt],
)
But this throws me an Exception and thus does not work.
I created my table like this:
object CategoriesTable : IntIdTable() {
val name = varchar("name", 255)
val categoryId = optReference("parent_category_id", CategoriesTable, onDelete = ReferenceOption.CASCADE) // As long as this foreign key references the primary key of the Poems table this is enough.
val createdAt = varchar("created_at", 255).default(LocalDateTime.now().toDatabaseString())
val updatedAt = varchar("updated_at", 255).default(LocalDateTime.now().toDatabaseString())
}
Is there a specific thing I'm missing regarding the relationship? Do I need to use another join type? Could someone give me a bit more info what exactly is going wrong and what I can do next 😅Johannes Gätjen
01/03/2024, 1:12 PMChristiano
01/03/2024, 1:32 PMval categoryId = optReference("parent_category_id", CategoriesTable, onDelete = ReferenceOption.CASCADE)
Or isn't that the same as what you mean? 🤔Johannes Gätjen
01/03/2024, 1:57 PMcategory = this.toCategory()
Christiano
01/03/2024, 2:03 PMorg.jetbrains.exposed.sql.Alias@bff5930e as there is no matching primary key/foreign key pair and constraint missing
Because of that the category = this.toCategory()
can't even run yet...Johannes Gätjen
01/03/2024, 2:07 PMCategoriesTable.join(
CategoriesTable.alias("parent"),
JoinType.INNER,
CategoriesTable.category,
CategoriesTable.id
)
Christiano
01/03/2024, 2:12 PMJohannes Gätjen
01/03/2024, 2:15 PMvar parent by CategoryDAO optionalReferencedOn CategoriesTable.parent
val children by CategoryDAO optionalReferrersOn CategoriesTable.parent
May be less performant though, depending on what you want to do.Christiano
01/03/2024, 2:17 PM