# Model Many to Many relationships

**URL:** <https://discourse.julialang.org/t/model-many-to-many-relationships/44967>\
**Category:** General Usage\
**Tags:** question\
**Created:** [August 14, 2020, 10:49pm UTC](https://discourse.julialang.org/t/model-many-to-many-relationships/44967 "2020-08-14T22:49:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alec\_Loudenback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alec_loudenback/32/278_2.png) [@Alec\_Loudenback](https://discourse.julialang.org/u/Alec_Loudenback)\
**Post date:** [August 14, 2020, 10:49pm UTC](https://discourse.julialang.org/t/model-many-to-many-relationships/44967/1 "2020-08-14T22:49:51Z")

</div>

If I have a many-to-many set of relationships that I want to model, what are performant and pleasant ways to do this?

E.g. `A`s can have many `B`s, and different `A`s have a relationship with the same `B`:

```julia

struct A
    contents
end

struct B
    val
end

a1 = A([])
a2 = A([])

b1 = B(42)

push!(a1.contents,b1)
push!(a2.contents,b1)

b1 = B(50)

a1.contents # still B(42)

```

I’d like to be able to manipulate `b1` and have `a1` and `a2` both reflect the change. Are there patterns/techniques to accommodate this?

---

<div class="post-metadata">

**Author:** ![lhnguyen-vn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lhnguyen-vn/32/15449_2.png) [@lhnguyen-vn](https://discourse.julialang.org/u/lhnguyen-vn)\
**Post date:** [August 14, 2020, 11:13pm UTC](https://discourse.julialang.org/t/model-many-to-many-relationships/44967/2 "2020-08-14T23:13:39Z")

</div>

The variable `b1` is rebound to a newly created `B(50)` in your code, but `a1` and `a2` do not update because they keep the (previously bound) value of `b1`, not the variable itself. Off the top of my head an easy way to achieve what you want is with mutable struct, but there might be performance caveats.

```julia
struct A
    contents
end

mutable struct B
    val
end

a1 = A([])
a2 = A([])

b1 = B(42)

push!(a1.contents,b1)
push!(a2.contents,b1)

b1.val = 50

```

---

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [August 15, 2020, 2:04am UTC](https://discourse.julialang.org/t/model-many-to-many-relationships/44967/3 "2020-08-15T02:04:37Z")

</div>

Perhaps you want to have each object just be itself, and then create a graph that represents the relationships among all of them?

---

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [August 15, 2020, 3:12am UTC](https://discourse.julialang.org/t/model-many-to-many-relationships/44967/4 "2020-08-15T03:12:48Z")

</div>

From 2020 juliacon:

[![](https://global.discourse-cdn.com/julialang/original/3X/1/9/19790b163e3a69cc5f16e6bc9acfb2071fc9e9c5.jpeg "JuliaCon 2020 | Salsa.jl: A framework for on-demand, incremental computation | Nathan Daly") ](https://www.youtube.com/watch?v=0uzrH2Ee494)
