#Mutual O2M and M2O relationship

15 messages · Page 1 of 1 (latest)

obtuse ibex
#

Hi!

I'm a bit confused with O2M and M2O relationship. I've got a Customer that has many stores and a store belongs to one customer. I can create a customer_id key on store with a M2O relationship and all goes well. Now I need to have this relation also on Customer. i.e. looking for all stores associated with one customer. And possibly have this relation taken from the previous one. Strapi can do that in a straightforward method. Can Directus do this easily? thanks!

white trench
#

When you create the m2o relationship on store, there is an option to create a corresponding (alias) field on the customer collection. In case you forgot that, you can just define the same relationship again "over" the existing one with that option checked, directus will detect what is already there and completes what is missing (maybe showing an error that you can just ignore). You can also add a o2m relationship to the customer collection pointing to the existing foreign key on store, the result will be the same, an alias field on customer listing all stores related to it. The "one" side of a relationship is at most a (virtual) alias field inside the directus app, just for data editing convenience, it is never an actual column in the db, foreign keys are always stored on the "many" side.

obtuse ibex
#

Thank you, that's very clear. My only doubt now is ...When I try to create e On to Many relation the field Foreign Key always stays greyed out. I really can't understand what am I missing...do you have an idea of what's going wrong ? Thank you very much

white trench
#

You have to define/select a related collection first, then the foreign key field gets enabled. Now, the menu to select an existing field as foreign key has a bug as far as I know, you cannot select one but this will be fixed. You can type the name of the related collection and foreign key manually and when it matches an existing one, the font color changes subtly.

obtuse ibex
#

I'm a bit confused. Trying to create a relation between Company - O2M - contacts. I add a contact field on Company, O2M relation with contact and then (all is greyed out) I select "id" as the foreign key... The response : "[INVALID_PAYLOAD] Field "id" in collection "contact" is a primary key". I don't know what to select as a foreign key ....

obtuse ibex
#

Another question is....in a M2A relationship between Company / Contacts / Store Roles I can only display the relation with ID's as you see in the screenshot. Can I have maybe a table with contacts names or emails and store roles names ? Thank you very much !

white trench
# obtuse ibex I'm a bit confused. Trying to create a relation between Company - O2M - contacts...

The error says it pretty clear, id is the primary key of contacts and so cannot be the foreign key for company at the same time. What you need is creating a foreign key (new field) in the contacts collection that can contain the primary key of company. Have a look at the easy data model introduction docs of directus: https://docs.directus.io/configuration/data-model.html#foreign-keys Maybe also have a look at my post about naming fields to have a nicer looking api and ui names: #725817819882586132 message

white trench
obtuse ibex
#

Ok I've done it!! 😅 Last doubt. Is there a way to put a property on a relation? I mean... I've got company and user. A user in a company has a certain role, in another a different role. So the role is an attribuite of the relation in my opinion. What is the smartest way to achieve this?

white trench
#

Yes, that is why an m2m junction collection can have additional fields, other than the two foreign keys and the surrogate primary key.

obtuse ibex
#

ok, I will try out. Is it possible to rename a collection easily?

white trench
#

Not possible at the moment, nor collections nor fields. This has been requested a lot and might get implemented in the future. If you really need to rename it and cannot delete and recreate it, you have to rename it in the db and then update all meta data in directus tables, which might get complicated if you consider revisions too.

white trench
# obtuse ibex Where do I put those fields ?

In the junction collection, the one that gets created with m2m and named collection1_collection2 by default, if you did not create it manually before. Edit it and add more fields. It is hidden on the collection page by default but you can make it visible in data model page. It is a collection/table like any other.

obtuse ibex
#

Thank you Manuel. I will try out. I came from Strapi wich is much more simple and much more limited in terms of relationship. Do we have an in depth guide of this argument or maybe a video? You are very kind