Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
B
beego
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
beego
Commits
c6cb1f92
Commit
c6cb1f92
authored
Oct 24, 2014
by
astaxie
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #870 from supar/add-column-default-attribute
Add column default attribute
parents
9c07332c
ab28edaf
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
53 additions
and
1 deletion
+53
-1
cmd_utils.go
orm/cmd_utils.go
+47
-1
models_info_f.go
orm/models_info_f.go
+6
-0
No files found.
orm/cmd_utils.go
View file @
c6cb1f92
...
...
@@ -104,7 +104,11 @@ func getColumnAddQuery(al *alias, fi *fieldInfo) string {
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.
...
...
@@ -155,6 +159,9 @@ func getDbCreateSql(al *alias) (sqls []string, tableIndexes map[string][]dbIndex
//if fi.initial.String() != "" {
// column += " DEFAULT " + fi.initial.String()
//}
// Append attribute DEFAULT
column
+=
getColumnDefault
(
fi
)
if
fi
.
unique
{
column
+=
" "
+
"UNIQUE"
...
...
@@ -239,3 +246,42 @@ func getDbCreateSql(al *alias) (sqls []string, tableIndexes map[string][]dbIndex
return
}
// Get string value for the attribute "DEFAULT" for the CREATE, ALTER commands
func
getColumnDefault
(
fi
*
fieldInfo
)
string
{
var
(
v
,
t
,
d
string
)
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
}
orm/models_info_f.go
View file @
c6cb1f92
...
...
@@ -116,6 +116,7 @@ type fieldInfo struct {
null
bool
index
bool
unique
bool
colDefault
bool
initial
StrTo
size
int
auto_now
bool
...
...
@@ -280,6 +281,11 @@ checkType:
fi
.
pk
=
attrs
[
"pk"
]
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
{
case
RelManyToMany
,
RelReverseMany
,
RelReverseOne
:
fi
.
null
=
false
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment