creating and updating an ObjectBox 1:n relationship


I am a bit confused with the documentation. Let’s stay with the customer order relationship, where a customer can have n orders.

If I create a new order for an existing customer and store this with

Customer customer = Customer();
customer.orders.add(Order()); // Order #3
customer.orders.add(Order()); // Order #4
// Puts customer and orders:
final customerId =<Customer>().put(customer);

I expect this to update the customer (overwriting all data) because I am using the id of the customer. And I guess, the new orders #3 and #4 are created as new orders and linked to the customer, because their order id is 0. But what happens with my orders I have stored previously (#1 & #2)? Do they remain linked to my customer? Or is this link overwritten as well and as such lost?


As is, your code example would create a new customer. To update an existing customer and its relation, first get the existing customer from the store (or re-use the previously put customer instance).

Customer customer =<Customer>().get(customerId);
customer.orders.add(Order()); // Order #3
customer.orders.add(Order()); // Order #4<Customer>().put(customer);

The documentation also shows how to get and remove objects from a ToMany relation.

Answered By – Uwe – ObjectBox

Answer Checked By – Candace Johnson (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *