oSumAtrIX
01/30/2024, 3:37 PMclass Announcement(id: EntityID<Int>) : IntEntity(id) {
companion object : IntEntityClass<Announcement>(AnnouncementsTable)
var author by AnnouncementsTable.author
var title by AnnouncementsTable.title
var content by AnnouncementsTable.content
var attachmentUrls by Attachment via AttachmentsTable
var channel by AnnouncementsTable.channel
var createdAt by AnnouncementsTable.createdAt
var archivedAt by AnnouncementsTable.archivedAt
var level by AnnouncementsTable.level
}
class Attachment(id: EntityID<Int>) : IntEntity(id) {
companion object : IntEntityClass<Attachment>(AttachmentsTable)
var url by AttachmentsTable.url
}
These are the entities I have now, but I get the exception, table does not reference sourceChantal Loncle
02/02/2024, 5:38 PMAttachmentsTable
. A foreign key can be generated on table creation in Exposed using `reference()`:
object AttachmentsTable : IntIdTable() {
val url = varchar("url", 256)
val announcement = reference("announcement", AnnouncementsTable)
}
// no changes needed for AnnouncementsTable
A corresponding field for this new column should be added to the entity Attachment
so that you can insert/access the referenced announcement (and its fields):
class Attachment(id: EntityID<Int>) : IntEntity(id) {
companion object : IntEntityClass<Attachment>(AttachmentsTable)
var url by AttachmentsTable.url
var announcement by Announcement referencedOn AttachmentsTable.announcement
}
If you also want to get all attachments for a single announcement, you just need to adjust the attachmentUrls
field in the Announcement
entity class:
val attachmentUrls by Attachment referrersOn AttachmentsTable.announcement
via
only comes into play for many-to-many relations that use a third intermediate table for reference storage.oSumAtrIX
02/09/2024, 2:24 AMoSumAtrIX
02/09/2024, 2:26 AMChantal Loncle
02/11/2024, 5:03 PMval announcement1 = Announcement.new {
author = "Author 1"
}
Attachment.new {
url = "url 1"
announcement = announcement1
}
Parent entities can also be created and referenced at the same time by nesting calls to new()
in the child entity:
Attachment.new {
url = "url 2"
announcement = Announcement.new {
author = "Author 2"
}
}
If you need to separately create a new Attachment and reference an Accouncement sometime after insertion, this means the relation column has to be nullable. So you'd have to use the optional variants of all the reference functions I mentioned previously, for example:
object AttachmentsTable : IntIdTable() {
val announcement: Column<EntityID<Int>?> = optReference("announcement", AnnouncementsTable)
// equivalent to:
// val announcement = reference("announcement", AnnouncementsTable).nullable()
}