From 92a238945056cbbe204e096d98fd76e1e01ab61d Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Tue, 16 Feb 2021 08:35:19 +0800 Subject: [PATCH] Fix create duplicated constraint, close #4090 --- schema/relationship.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/schema/relationship.go b/schema/relationship.go index 1aa2d11a..606e722a 100644 --- a/schema/relationship.go +++ b/schema/relationship.go @@ -512,6 +512,24 @@ func (rel *Relationship) ParseConstraint() *Constraint { return nil } + if rel.Type == BelongsTo { + for _, r := range rel.FieldSchema.Relationships.Relations { + if r.FieldSchema == rel.Schema && len(rel.References) == len(r.References) { + matched := true + for idx, ref := range r.References { + if !(rel.References[idx].PrimaryKey == ref.PrimaryKey && rel.References[idx].ForeignKey == ref.ForeignKey && + rel.References[idx].PrimaryValue == ref.PrimaryValue) { + matched = false + } + } + + if matched { + return nil + } + } + } + } + var ( name string idx = strings.Index(str, ",")