In Firestore we had customers/{id}/orders/{id}. Is there an equivalent, or should each order just store a customer_id string?
Coming from Firestore: how do I model subcollections?
Common question, written up by the Supero team.
1 answer
The closest equivalent is a parent relationship. Set parent_type on the child schema to the container schema's bare name (customer), then pass the parent when you create:
customer = org.crud.get("customer", customer_uuid)
order = org.crud.create("order", parent=customer, name="order-001", total=199.99)The SDK fills in fq_name and parent_uuid. Over REST, send parent_uuid and parent_type in the create body. To list one customer's orders, call GET list with ?parent_id=<customer uuid>, or put parent_id in a /list-bulk body when you also need filters.
When the link isn't ownership, such as an order pointing at a coupon, use a reference instead: declare it in the schema's references[], set it with ref-update, and read it with /refs and /back-refs.
Settle this before you import. parent_uuid can't be changed after create, and a schema update that changes parent_type while records exist is blocked with a 422.
Your answer
Know the answer? Sign in to post it. It takes an email address and a 6-digit code.
Sign in to answer