mirror of
https://github.com/astaxie/beego.git
synced 2024-11-22 18:00:55 +00:00
Skip add DEFAULT if the field is in relations (rel or reverse)
This commit is contained in:
parent
2a85c79ce5
commit
c34c514bba
@ -239,3 +239,46 @@ func getDbCreateSql(al *alias) (sqls []string, tableIndexes map[string][]dbIndex
|
|||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get string value for the attribute "DEFAULT" for the CREATE, ALTER commands
|
||||||
|
func getColumnDefault(fi *fieldInfo) string {
|
||||||
|
var (
|
||||||
|
v, t, d string
|
||||||
|
)
|
||||||
|
|
||||||
|
// Skip default attribute if field is in relations
|
||||||
|
if fi.rel || fi.reverse {
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
|
||||||
|
t = " DEFAULT '%s' "
|
||||||
|
|
||||||
|
// These defaults will be useful if there no config value orm:"default" and NOT NULL is on
|
||||||
|
switch fi.fieldType {
|
||||||
|
case TypeDateField:
|
||||||
|
d = "0000-00-00"
|
||||||
|
|
||||||
|
case TypeDateTimeField:
|
||||||
|
d = "0000-00-00 00:00:00"
|
||||||
|
|
||||||
|
case TypeBooleanField, TypeBitField, TypeSmallIntegerField, TypeIntegerField,
|
||||||
|
TypeBigIntegerField, TypePositiveBitField, TypePositiveSmallIntegerField,
|
||||||
|
TypePositiveIntegerField, TypePositiveBigIntegerField, TypeFloatField,
|
||||||
|
TypeDecimalField:
|
||||||
|
d = "0"
|
||||||
|
}
|
||||||
|
|
||||||
|
if fi.colDefault {
|
||||||
|
if !fi.initial.Exist() {
|
||||||
|
v = fmt.Sprintf(t, "")
|
||||||
|
} else {
|
||||||
|
v = fmt.Sprintf(t, fi.initial.String())
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if !fi.null {
|
||||||
|
v = fmt.Sprintf(t, d)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user