# 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:** 1\
**Showing post:** 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: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 =#)]))

```

---

_[View the full topic](https://discourse.julialang.org/t/best-way-to-make-has-many-belongs-to-relation-for-structs/7937)._
