I have two models that are have a weird interaction:
@Model public final class Position: Equatable, Identifiable {
var zone: ZoneModel?
@Relationship(deleteRule: .cascade, inverse: \Item.positions)
var items = [Item]()
var name: String = ""
var weight: Int = 0
var weightAllowed: Int = 0
init(name: String, weightAllowed: Int) {
self.name = name
self.weightAllowed = weightAllowed
}
}
@Model public final class Item: Equatable, Identifiable {
var form: FormModel?
var positions: [Position]()
var count: Int = 0
var weight: Int = 0
init() {}
}
There are many other tables that are all connected, but these are the ones where the problems arise. The PositionModel is able to properly store and persist the items: [ItemModel] variable, but the ItemModel runs into a problem after the app closes.
I am getting this error in my SQL Stack Trace
no such column: t1.Z_6POSITIONS in "SELECT 0, t0.Z_PK FROM Z_5POSITIONS t1 JOIN ZPOSITION t0 ON t0.Z_PK = t1.Z_6POSITIONS WHERE t1.Z_5ITEMS = ? "
When looking at my sqlite database I see that the table name is not Z_5POSITIONS.Z_6POSITIONS
, but it is Z_5POSITIONS.Z_7POSITIONS
.
Index T-SQL:
CREATE INDEX "Z_5POSITIONS_Z_7POSITIONS_INDEX" ON "Z_5POSITIONS" (
"Z_7POSITIONS",
"Z_5ITEMS"
);
For extra context here is the SQL Stack Trace:
CoreData: annotation: Connecting to sqlite database file at "/~/default.store"
CoreData: sql: SELECT TBL_NAME FROM SQLITE_MASTER WHERE TBL_NAME = 'Z_METADATA'
TraceSQL(0x110e76a60): SELECT TBL_NAME FROM SQLITE_MASTER WHERE TBL_NAME = 'Z_METADATA'
CoreData: sql: pragma recursive_triggers=1
TraceSQL(0x110e76a60): pragma recursive_triggers=1
CoreData: sql: pragma journal_mode=wal
TraceSQL(0x110e76a60): pragma journal_mode=wal
CoreData: sql: SELECT 0, t0.Z_PK FROM Z_5POSITIONS t1 JOIN ZPOSITION t0 ON t0.Z_PK = t1.Z_6POSITIONS WHERE t1.Z_5ITEMS = ?
no such column: t1.Z_6POSITIONS in "SELECT 0, t0.Z_PK FROM Z_5POSITIONS t1 JOIN ZPOSITION t0 ON t0.Z_PK = t1.Z_6POSITIONS WHERE t1.Z_5ITEMS = ? "
CoreData: annotation: Disconnecting from sqlite database due to an error.
I have on automatic save, and also just to be sure and test it I am manually saving the added on ItemModel object. Every other SwiftData object is being properly saved, including the Items to the Position side to the many-to-many relationship, but the inverse isn't working.
Header(vm: vm, onSubmit: {
if vm.selected == nil {
vm.new.updateWeight(with: oc)
report.form?.addItem(vm.new)
do {
try context.save()
Logger.write("Save Successful.")
} catch {
Logger.write("Save error: \(error)")
}
vm.new = ItemModel()
} else { vm.selected = nil }
})
.padding(.vertical, 10)
I always get a successful save. But every time I close and reopen the app I run into the above issue.
Does anyone have any insight into this or maybe can spot something I have wrong off a look?