Hey all. So I have an application where on a certain view controller I call a few static methods which updates certain columns on certain tables. For some reason this one column just will not update not matter how many things ive tried.
Here is the method:
func addSession() {
if let days = locationInputView?.getDays(), let hours = locationInputView?.getHours(), let locationName = locationInputView?.getLocationName() {
locationInputView?.selectedDays = [days]
locationInputView?.selectedHours = [hours]
setHooModel(model: HoursOfOperation.insertAndInjectNewHoo()) // TODO: Write this method as two seperate methods and apply to this VC appropriately
if let view = locationInputView {
if let locationModel = locationModel {
if let hooModel = hooModel {
HoursOfOperation.setHooDaysHours(hooid: hooModel.id, selectedDays: view.selectedDays, selectedHours: view.selectedHours)
updateHooModel(model: hooModel)
Location.setLocationNameFromid(locationName: locationName, id: locationModel.id)
Location.setHooidForLocation(locationid: locationModel.id, hooid: hooModel.id)
updateLocationModel(model: locationModel)
print("add session: \(self.locationModel)")
} else {
print("1: Optional found nil")
}
} else {
print("2: Optional found nil")
}
} else {
print("3: Optional found nil")
}
} else {
print("4: Optional found nil")
}
}
The updateLocationModel()
and like methods simply retrieves the data from the db and re injects them into my data models
static func setHooidForLocation(locationid: String, hooid: String) {
do {
try AppDatabase.shared.dbwriter.write({ db in
if var location = try Location.fetchOne(db, key: locationid) {
location.hooid = hooid
if location.hooid != nil {
try location.update(db)
//try db.execute(sql: "UPDATE location SET hooid = ? WHERE id = ?", arguments: [hooid, locationid]) // FIXME: Write with raw SQL and hope for the best
}
}
})
} catch {
fatalError()
}
}
and finally the relevant pieces of the schema:
try db.create(table: "Location", body: { t in
t.column("id", .text).primaryKey()
t.column("locationName", .text)
t.column("addressString", .text).notNull()
t.column("latitude", .double).notNull()
t.column("longitude", .double).notNull()
t.column("hooid", .text).references("HoursOfOperation", column: "id", onDelete: nil, onUpdate: nil, deferred: false)
try db.create(table: "HoursOfOperation", body: { t in
t.column("id", .text).primaryKey()
t.column("dayOfWeek", .text).notNull()
t.column("timeOfDay", .text).notNull()
})
Things I have tried:
- writing raw SQL for the update
- ensuring ALL optionals are not nil, unwrapped and have expected values
- changing all created db connections to dbQueue from dbPool in case of concurrency issues
- moving
setHooidForLocation()
to another method in case of issues involving scope
I should mention I do not and never have had any problems with updating columns with or without foreign keys relating to other parts of my db structure.