OOro

Relation Definition

Define BelongsTo, HasOne, HasMany, ManyToMany, and dynamic relations with model methods.

Oro relations are model methods returning oro.Relation. They are not embedded fields on the struct.

func (article Article) Comments() oro.Relation {
    return oro.HasMany(article, "Comments", "Comment").
        ForeignKey("ArticleID").
        ReferenceKey("ID")
}

This keeps models split across packages without forcing import cycles.

HasOne

func (article Article) Cover() oro.Relation {
    return oro.HasOne(article, "Cover", "Image").
        ForeignKey("ArticleID").
        ReferenceKey("ID")
}

HasMany

func (article Article) Comments() oro.Relation {
    return oro.HasMany(article, "Comments", "Comment").ForeignKey("ArticleID")
}

BelongsTo

func (comment Comment) Article() oro.Relation {
    return oro.BelongsTo(comment, "Article", "Article").
        ForeignKey("ArticleID").
        ReferenceKey("ID")
}

Many-to-many

func (article Article) Tags() oro.Relation {
    return oro.ManyToMany(article, "Tags", "Tag").
        JoinTable("article_tags").
        ForeignKey("ArticleID").
        RelatedKey("TagID")
}

The join table can also be represented by a model when it has business fields.

Dynamic relation names

Static relation methods are preferred. String names are supported where dynamic many-to-many or plugin-defined relations need them.

Edit this page