From 1e31b770ca5ca69fa26ebb4f33eb72cb947c12aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vojt=C4=9Bch=20K=C3=A1n=C4=9B?= Date: Wed, 21 Apr 2021 12:10:28 +0200 Subject: [PATCH] Add OnDelete annotation to each edge --- pkg/model/ent/schema/choice.go | 6 +++++- pkg/model/ent/schema/game.go | 11 +++++++++-- pkg/model/ent/schema/player.go | 6 +++++- pkg/model/ent/schema/question.go | 11 +++++++++-- pkg/model/ent/schema/session.go | 11 +++++++++-- 5 files changed, 37 insertions(+), 8 deletions(-) diff --git a/pkg/model/ent/schema/choice.go b/pkg/model/ent/schema/choice.go index cb9773a..58d3ddb 100644 --- a/pkg/model/ent/schema/choice.go +++ b/pkg/model/ent/schema/choice.go @@ -2,6 +2,7 @@ package schema import ( "entgo.io/ent" + "entgo.io/ent/dialect/entsql" "entgo.io/ent/schema/edge" "entgo.io/ent/schema/field" "github.com/google/uuid" @@ -29,6 +30,9 @@ func (Choice) Edges() []ent.Edge { Ref("choices"). Unique(). Required(), - edge.To("answers", Answer.Type), + edge.To("answers", Answer.Type). + Annotations(entsql.Annotation{ + OnDelete: entsql.Restrict, + }), } } diff --git a/pkg/model/ent/schema/game.go b/pkg/model/ent/schema/game.go index 973bbac..28df8e3 100644 --- a/pkg/model/ent/schema/game.go +++ b/pkg/model/ent/schema/game.go @@ -2,6 +2,7 @@ package schema import ( "entgo.io/ent" + "entgo.io/ent/dialect/entsql" "entgo.io/ent/schema/edge" "entgo.io/ent/schema/field" "github.com/google/uuid" @@ -23,7 +24,13 @@ func (Game) Fields() []ent.Field { func (Game) Edges() []ent.Edge { return []ent.Edge{ - edge.To("sessions", Session.Type), - edge.To("questions", Question.Type), + edge.To("sessions", Session.Type). + Annotations(entsql.Annotation{ + OnDelete: entsql.Restrict, + }), + edge.To("questions", Question.Type). + Annotations(entsql.Annotation{ + OnDelete: entsql.Cascade, + }), } } diff --git a/pkg/model/ent/schema/player.go b/pkg/model/ent/schema/player.go index b08c8a6..885b418 100644 --- a/pkg/model/ent/schema/player.go +++ b/pkg/model/ent/schema/player.go @@ -2,6 +2,7 @@ package schema import ( "entgo.io/ent" + "entgo.io/ent/dialect/entsql" "entgo.io/ent/schema/edge" "entgo.io/ent/schema/field" "entgo.io/ent/schema/index" @@ -34,6 +35,9 @@ func (Player) Edges() []ent.Edge { Ref("players"). Unique(). Required(), - edge.To("answers", Answer.Type), + edge.To("answers", Answer.Type). + Annotations(entsql.Annotation{ + OnDelete: entsql.Cascade, + }), } } diff --git a/pkg/model/ent/schema/question.go b/pkg/model/ent/schema/question.go index be62384..f6847c4 100644 --- a/pkg/model/ent/schema/question.go +++ b/pkg/model/ent/schema/question.go @@ -2,6 +2,7 @@ package schema import ( "entgo.io/ent" + "entgo.io/ent/dialect/entsql" "entgo.io/ent/schema/edge" "entgo.io/ent/schema/field" "entgo.io/ent/schema/index" @@ -33,7 +34,13 @@ func (Question) Edges() []ent.Edge { Ref("questions"). Unique(). Required(), - edge.To("choices", Choice.Type), - edge.To("asked", AskedQuestion.Type), + edge.To("choices", Choice.Type). + Annotations(entsql.Annotation{ + OnDelete: entsql.Cascade, + }), + edge.To("asked", AskedQuestion.Type). + Annotations(entsql.Annotation{ + OnDelete: entsql.Restrict, + }), } } diff --git a/pkg/model/ent/schema/session.go b/pkg/model/ent/schema/session.go index 2d5f89c..a79cbaa 100644 --- a/pkg/model/ent/schema/session.go +++ b/pkg/model/ent/schema/session.go @@ -2,6 +2,7 @@ package schema import ( "entgo.io/ent" + "entgo.io/ent/dialect/entsql" "entgo.io/ent/schema/edge" "entgo.io/ent/schema/field" "github.com/google/uuid" @@ -30,7 +31,13 @@ func (Session) Edges() []ent.Edge { Ref("sessions"). Unique(). Required(), - edge.To("players", Player.Type), - edge.To("askedQuestions", AskedQuestion.Type), + edge.To("players", Player.Type). + Annotations(entsql.Annotation{ + OnDelete: entsql.Cascade, + }), + edge.To("askedQuestions", AskedQuestion.Type). + Annotations(entsql.Annotation{ + OnDelete: entsql.Cascade, + }), } }