How do I create a Many-to-Many Doctype mapping

Hello Everyone,

I have a usecase where I need to have many-to-many mapping between 2 Doctypes as follows:

  • I have 2 Doctypes Plan & Control (It is many to many because many Controls can map to multiple Plans and vice versa)
  • I want to be able to create a Plan with(out) a Control and vice versa.

How can I implement this?

Note: I already explored using child table and creating intermediate table (with link types) but they dont fit my use case. With intermediate Doctypes I get an error that I am not sure how to fix as shown below.

For creatimg many to one fields in Frappe you had to use link fieldtype and in option select the doctype from which you had to join the other table/doctype.

Hello @Mohtashim_Shoaib ,

Thanks for your answer, but I want a Many-to-Many not Many-to-One for this example.

For the case you mentioned, I currently just use the Doctype Link feature and that works ok for me.

Child tables with linking doctypes is indeed the correct way do many-to-many. I don’t know what’s going on with the error you’re showing, and it’s hard to say much more without context.

Yes, you can do with child table this thing but its depend on your use case which is best fit in your scenario.

@peterg Do you know how the Customer - Address & Contact relationship is defined?
That is exactly the type of mapping that I am looking for.

About the Child table and linking doctype option, but I will investigate the current error and see if there is a way to provide more context.

The Address and Contact interfaces on the Customer doctype uses a custom HTML field with doctype-specific javascript.

The actual data representation is managed by a child table with a linking doctype, like what I’m describing in my last post (the links child table in the Address or Contact doctype).

Thanks for that clarification, I found what I did wrong, I went to the child Table (Control Details) and set the Naming rule to be fieldName:[field], so that made the link field have the same value as the default primary key field of name, which obviously has a unique constraint in the DB.

For the sake of information for anyone else that is looking for a similar solution, these are my steps.

I did the following:

  • Created the Plan Doctype and the Control Doctype as standalone types
  • Then I created a Control Details is_child Doctype with a single link field to Control Doctype. Make sure that you dont change the default Settings for Naming.
1 Like