Fix create duplicated constraint, close #4090

This commit is contained in:
Jinzhu 2021-02-16 08:35:19 +08:00
parent 628a0ae707
commit 92a2389450
1 changed files with 18 additions and 0 deletions

View File

@ -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, ",")