Golang/ORM

[Entgo] About Annotations

GenieLove! 2022. 9. 3. 23:36
728x90
반응형

Annotations

fields 및 edges 와 같은 스키마 객체에 metadata를 첨부하고 외부 템블릿에 삽입할 수 있다.

Annotation은 json(struct, map, slice)으로 직렬화 할 수 있는 Go 타입이어야 하고 annotation 인터페이스를 상속해야 된다.

Custom Table Name

테이블 이름을 직접 지정하는 방법

func (User) Annotations() []schema.Annotation {
    return []schema.Annotation{
        entsql.Annotation{Table: "user"},
    }
}

Foreign Keys Configuration

외래키 설정을 직접 지정하는 방법

func (User) Edges() []ent.Edge {
    return []ent.Edge{
        edge.To("posts", Post.Type).
            Annotations(entsql.Annotation{
                OnDelete: entsql.Cascade,//외래키로 지정된 것에 onDelete옵션을 추가할 수 있다.
            }),
    }
}
728x90
반응형

'Golang > ORM' 카테고리의 다른 글

[GORM] Query  (0) 2022.09.28
[Entgo] CRUD API(작성 중)  (0) 2022.09.12
[Entgo] About Mixin  (0) 2022.09.01
[Entgo] About Indexes  (0) 2022.08.30
[Entgo] About Edges  (0) 2022.08.29