mirror of
https://github.com/astaxie/beego.git
synced 2024-11-25 20:41:29 +00:00
Add column DEFAULT attribute. Do not add if field is key or in
relations.
This commit is contained in:
parent
c34c514bba
commit
6f5162461e
@ -104,7 +104,11 @@ func getColumnAddQuery(al *alias, fi *fieldInfo) string {
|
|||||||
typ += " " + "NOT NULL"
|
typ += " " + "NOT NULL"
|
||||||
}
|
}
|
||||||
|
|
||||||
return fmt.Sprintf("ALTER TABLE %s%s%s ADD COLUMN %s%s%s %s", Q, fi.mi.table, Q, Q, fi.column, Q, typ)
|
return fmt.Sprintf("ALTER TABLE %s%s%s ADD COLUMN %s%s%s %s %s",
|
||||||
|
Q, fi.mi.table, Q,
|
||||||
|
Q, fi.column, Q,
|
||||||
|
typ, getColumnDefault(fi),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// create database creation string.
|
// create database creation string.
|
||||||
@ -155,6 +159,9 @@ func getDbCreateSql(al *alias) (sqls []string, tableIndexes map[string][]dbIndex
|
|||||||
//if fi.initial.String() != "" {
|
//if fi.initial.String() != "" {
|
||||||
// column += " DEFAULT " + fi.initial.String()
|
// column += " DEFAULT " + fi.initial.String()
|
||||||
//}
|
//}
|
||||||
|
|
||||||
|
// Append attribute DEFAULT
|
||||||
|
column += getColumnDefault(fi)
|
||||||
|
|
||||||
if fi.unique {
|
if fi.unique {
|
||||||
column += " " + "UNIQUE"
|
column += " " + "UNIQUE"
|
||||||
@ -240,6 +247,7 @@ func getDbCreateSql(al *alias) (sqls []string, tableIndexes map[string][]dbIndex
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Get string value for the attribute "DEFAULT" for the CREATE, ALTER commands
|
// Get string value for the attribute "DEFAULT" for the CREATE, ALTER commands
|
||||||
func getColumnDefault(fi *fieldInfo) string {
|
func getColumnDefault(fi *fieldInfo) string {
|
||||||
var (
|
var (
|
||||||
@ -255,19 +263,19 @@ func getColumnDefault(fi *fieldInfo) string {
|
|||||||
|
|
||||||
// These defaults will be useful if there no config value orm:"default" and NOT NULL is on
|
// These defaults will be useful if there no config value orm:"default" and NOT NULL is on
|
||||||
switch fi.fieldType {
|
switch fi.fieldType {
|
||||||
case TypeDateField:
|
case TypeDateField:
|
||||||
d = "0000-00-00"
|
d = "0000-00-00"
|
||||||
|
|
||||||
case TypeDateTimeField:
|
case TypeDateTimeField:
|
||||||
d = "0000-00-00 00:00:00"
|
d = "0000-00-00 00:00:00"
|
||||||
|
|
||||||
case TypeBooleanField, TypeBitField, TypeSmallIntegerField, TypeIntegerField,
|
case TypeBooleanField, TypeBitField, TypeSmallIntegerField, TypeIntegerField,
|
||||||
TypeBigIntegerField, TypePositiveBitField, TypePositiveSmallIntegerField,
|
TypeBigIntegerField, TypePositiveBitField, TypePositiveSmallIntegerField,
|
||||||
TypePositiveIntegerField, TypePositiveBigIntegerField, TypeFloatField,
|
TypePositiveIntegerField, TypePositiveBigIntegerField, TypeFloatField,
|
||||||
TypeDecimalField:
|
TypeDecimalField:
|
||||||
d = "0"
|
d = "0"
|
||||||
}
|
}
|
||||||
|
|
||||||
if fi.colDefault {
|
if fi.colDefault {
|
||||||
if !fi.initial.Exist() {
|
if !fi.initial.Exist() {
|
||||||
v = fmt.Sprintf(t, "")
|
v = fmt.Sprintf(t, "")
|
||||||
|
@ -116,6 +116,7 @@ type fieldInfo struct {
|
|||||||
null bool
|
null bool
|
||||||
index bool
|
index bool
|
||||||
unique bool
|
unique bool
|
||||||
|
colDefault bool
|
||||||
initial StrTo
|
initial StrTo
|
||||||
size int
|
size int
|
||||||
auto_now bool
|
auto_now bool
|
||||||
@ -280,6 +281,11 @@ checkType:
|
|||||||
fi.pk = attrs["pk"]
|
fi.pk = attrs["pk"]
|
||||||
fi.unique = attrs["unique"]
|
fi.unique = attrs["unique"]
|
||||||
|
|
||||||
|
// Mark object property if there is attribute "default" in the orm configuration
|
||||||
|
if _, ok := tags["default"]; ok {
|
||||||
|
fi.colDefault = true
|
||||||
|
}
|
||||||
|
|
||||||
switch fieldType {
|
switch fieldType {
|
||||||
case RelManyToMany, RelReverseMany, RelReverseOne:
|
case RelManyToMany, RelReverseMany, RelReverseOne:
|
||||||
fi.null = false
|
fi.null = false
|
||||||
|
Loading…
Reference in New Issue
Block a user