# Best way to make has\_many/belongs\_to relation for structs?

**URL:** <https://discourse.julialang.org/t/best-way-to-make-has-many-belongs-to-relation-for-structs/7937>\
**Category:** General Usage\
**Tags:** question\
**Created:** [December 23, 2017, 5:23am UTC](https://discourse.julialang.org/t/best-way-to-make-has-many-belongs-to-relation-for-structs/7937 "2017-12-23T05:23:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![djsegal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/djsegal/32/13752_2.png) [@djsegal](https://discourse.julialang.org/u/djsegal)\
**Post date:** [December 23, 2017, 5:23am UTC](https://discourse.julialang.org/t/best-way-to-make-has-many-belongs-to-relation-for-structs/7937/1 "2017-12-23T05:23:02Z")

</div>

Let’s say you have two types of structs:

- Users
- Posts

* * *

Users can have many posts. But each post belongs to only one user.

How would you do this in structs?

Some pseudocode:

```julia
struct User
  posts::AbstractArray{Post}
end

struct Post
   author::User
end

```

* * *

The simplest way I could think of is just removing `{Post}` from abstract array, but that’s cheating

---

<div class="post-metadata">

**Author:** ![djsegal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/djsegal/32/13752_2.png) [@djsegal](https://discourse.julialang.org/u/djsegal)\
**Post date:** [December 23, 2017, 5:24am UTC](https://discourse.julialang.org/t/best-way-to-make-has-many-belongs-to-relation-for-structs/7937/2 "2017-12-23T05:24:49Z")

</div>

Whoops. Did it again:

[https://github.com/JuliaLang/julia/issues/269](https://github.com/JuliaLang/julia/issues/269)

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [December 23, 2017, 6:36am UTC](https://discourse.julialang.org/t/best-way-to-make-has-many-belongs-to-relation-for-structs/7937/3 "2017-12-23T06:36:17Z")

</div>

How about:

```julia
julia> abstract type AbstractPost end

julia> struct User{V <: Vector{<:AbstractPost}}
         posts::V
       end

julia> struct Post <: AbstractPost
         user::User{Vector{Post}}
       end

julia> u1 = User(Post[])
User{Array{Post,1}}(Post[])

julia> p1 = Post(u1)
Post(User{Array{Post,1}}(Post[]))

julia> push!(u1.posts, p1)
1-element Array{Post,1}:
 Post(User{Array{Post,1}}(Post[Post(#= circular reference @-3 =#)]))

```

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [December 23, 2017, 6:48am UTC](https://discourse.julialang.org/t/best-way-to-make-has-many-belongs-to-relation-for-structs/7937/4 "2017-12-23T06:48:12Z")

</div>

(note that the `AbstractPost` supertype isn’t necessary to make this work, but I think it’s a nice indicator of intent in the absence of being able to write the circular definition you actually want)
