box.Puthas been executed on the first order, the
1because we're using pointers (
Customer *Customerfield) so Put could update the variable when it has inserted the Customer. Note that this wouldn't be possible if we were using copies (
Customer Customerfield) and in that case you should insert the customer manually into it's box first (or use an existing customer selected from the database).
linkannotation this time because ObjectBox wouldn't know how to store a slice of structs by itself anyway so it assumes it must be a many-to-may relation. They're stored when you put the source entity and loaded when you read it from the database, unless you specify a
lazyannotation in which case, they're loaded manually, using
student.Teachersslice to reflect the new state (i.e. remove element, add elements, etc) and
box.Put(student). Note that if you want to change actual teacher data (e.g. change teachers name), you need to update the teacher entity itself, not just change it in one of the student.Teachers slice.
Teachersfield would be
niland you can work with the student as you wish, changing it and saving and the list of assigned teachers wouldn't change as long as the
nil. If it wasn't
nil, but a slice of Teachers instead, ObjectBox would recognize this as an update of the field and replace the relational links.
Teachers, we need to first load them. ObjectBox has generated a helper method just for that
Teachers, we can either overwrite the slice with completely new data (new slice), or if we want to keep the original data and update it, e.g. change a few items, we need to load them first the same way as when reading (above).