mirror of https://github.com/go-gorm/gorm.git
Check data type if copyable before change reference field's type
This commit is contained in:
parent
f0d0bbbc10
commit
70e93e73d8
|
@ -163,7 +163,9 @@ func (schema *Schema) buildPolymorphicRelation(relation *Relationship, field *Fi
|
||||||
}
|
}
|
||||||
|
|
||||||
// use same data type for foreign keys
|
// use same data type for foreign keys
|
||||||
|
if copyableDataType(primaryKeyField.DataType) {
|
||||||
relation.Polymorphic.PolymorphicID.DataType = primaryKeyField.DataType
|
relation.Polymorphic.PolymorphicID.DataType = primaryKeyField.DataType
|
||||||
|
}
|
||||||
relation.Polymorphic.PolymorphicID.GORMDataType = primaryKeyField.GORMDataType
|
relation.Polymorphic.PolymorphicID.GORMDataType = primaryKeyField.GORMDataType
|
||||||
if relation.Polymorphic.PolymorphicID.Size == 0 {
|
if relation.Polymorphic.PolymorphicID.Size == 0 {
|
||||||
relation.Polymorphic.PolymorphicID.Size = primaryKeyField.Size
|
relation.Polymorphic.PolymorphicID.Size = primaryKeyField.Size
|
||||||
|
@ -302,7 +304,9 @@ func (schema *Schema) buildMany2ManyRelation(relation *Relationship, field *Fiel
|
||||||
for _, f := range relation.JoinTable.Fields {
|
for _, f := range relation.JoinTable.Fields {
|
||||||
if f.Creatable || f.Readable || f.Updatable {
|
if f.Creatable || f.Readable || f.Updatable {
|
||||||
// use same data type for foreign keys
|
// use same data type for foreign keys
|
||||||
|
if copyableDataType(fieldsMap[f.Name].DataType) {
|
||||||
f.DataType = fieldsMap[f.Name].DataType
|
f.DataType = fieldsMap[f.Name].DataType
|
||||||
|
}
|
||||||
f.GORMDataType = fieldsMap[f.Name].GORMDataType
|
f.GORMDataType = fieldsMap[f.Name].GORMDataType
|
||||||
if f.Size == 0 {
|
if f.Size == 0 {
|
||||||
f.Size = fieldsMap[f.Name].Size
|
f.Size = fieldsMap[f.Name].Size
|
||||||
|
@ -472,7 +476,9 @@ func (schema *Schema) guessRelation(relation *Relationship, field *Field, cgl gu
|
||||||
// build references
|
// build references
|
||||||
for idx, foreignField := range foreignFields {
|
for idx, foreignField := range foreignFields {
|
||||||
// use same data type for foreign keys
|
// use same data type for foreign keys
|
||||||
|
if copyableDataType(primaryFields[idx].DataType) {
|
||||||
foreignField.DataType = primaryFields[idx].DataType
|
foreignField.DataType = primaryFields[idx].DataType
|
||||||
|
}
|
||||||
foreignField.GORMDataType = primaryFields[idx].GORMDataType
|
foreignField.GORMDataType = primaryFields[idx].GORMDataType
|
||||||
if foreignField.Size == 0 {
|
if foreignField.Size == 0 {
|
||||||
foreignField.Size = primaryFields[idx].Size
|
foreignField.Size = primaryFields[idx].Size
|
||||||
|
@ -614,3 +620,12 @@ func (rel *Relationship) ToQueryConditions(reflectValue reflect.Value) (conds []
|
||||||
conds = append(conds, clause.IN{Column: column, Values: values})
|
conds = append(conds, clause.IN{Column: column, Values: values})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func copyableDataType(str DataType) bool {
|
||||||
|
for _, s := range []string{"auto_increment", "primary key"} {
|
||||||
|
if strings.Contains(strings.ToLower(string(str)), s) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue