What are your takes on o2o relations?

Hi everyone,

I’m curious about the most “Directussy” way to handle a one-to-one-like relation.

Example: collections restaurants and rocations. Each restaurant has exactly one location (restaurant.location).

  • Using O2M returns an array with one item via the API, which feels awkward.

  • Using M2O returns a single object, this makes locations the “parent” and cascade deletion is available only for restaurants.

What does the team/community think is the cleanest, most Directus-native approach for this pattern?

1 Answer

1

I’d set up a location m2o column from restaurant.locationlocations.id, then make the location column unique so a location can only ever be used on a restaurant once. That also means you can cascade delete the location when deleting the restaurant.

If you have no other planned relationships to locations, you could also consider just making them primitive fields directly on the restaurant, but that’s a preference not a requirement

Thanks for your reply ! I see your point about the m2o. "many restaurant" pointing to "one location", BUT restaurant.location must be unique. when fetching the data, restaurant.location is an object , not an array. it makes sense! then you say That also means you can cascade delete the location when deleting the restaurant. That is where I'm lost. When I set it up, I can only set cascade deletetion on the "many" side of the relation. "many restaurant" can be auto deleted when we delete "one location" feel like there is something I'm missing here... I'm really confused !

Ohh did I get my order reversed? Sorry! Easy to confuse the directionality in databases sometimes..