Modelling two dependent many-to-many relationships
Schematically:

CREATE TABLE users (id, 
                    name,
                    PK (id));

CREATE TABLE user_foo (user_id,
                       foo_id,
                       PK (user_id, foo_id),
                       FK (user_id) REF users (id),
                       FK (foo_id) REF foo (id));

CREATE TABLE foo (id, 
                  name,  
                  ord,
                  PK (id),  
                  KEY (id, ord));

CREATE TABLE foo_bar (foo_id,  
                      ord AS (1) VIRTUAL,  
                      bar_id,
                      PK (bar_id, foo_id),
                      FK (foo_id, ord) REF foo (id, ord),
                      FK (bar_id) REF bar (id));

CREATE TABLE bar (id,  
                  name,
                  PK (id));



  One foo may also have many users so the pivot tables are necessary.


Taken into account.


  foo and bar are ManyToMany relations


Taken into account.