final class BudgetItem: Model {
static let schema = "budget_items"
@ID(key: .id)
var id: UUID?
@Field(key: "name")
var name: String
@Parent(key: "budget_category_id")
var budgetCategory: BudgetCategory
@Field(key: "planned")
var planned: Double
@Field(key: "note")
var note: String
@OptionalField(key: "due_date")
var dueDate: Date?
@Children(for: \.$budgetItem)
var transactions: [Transaction]
init() {}
init(
id: UUID? = nil,
name: String,
budgetCategoryID: UUID,
planned: Double,
note: String,
dueDate: Date?
) {
self.id = id
self.name = name
self.$budgetCategory.id = budgetCategoryID
self.planned = planned
self.note = note
self.dueDate = dueDate
}
}
Then I have my Transaction object like so:
final class Transaction: Model {
static var schema: String = "transactions"
@ID(key: .id)
var id: UUID?
@Field(key: "name")
var name: String
@Parent(key: "budget_item_id")
var budgetItem: BudgetItem
@Field(key: "amount")
var amount: Double
@Field(key: "date")
var date: Date
@Enum(key: "transaction_type")
var transactionType: TransactionType
init() { }
init(
id: UUID? = nil,
name: String,
budgetItemID: UUID,
amount: Double,
date: Date,
transactionType: TransactionType
) {
self.id = id
self.name = name
self.$budgetItem.id = budgetItemID
self.date = date
self.transactionType = transactionType
}
}
My migrations work as expected, and my tables and columns look like they should.
!