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
c81bbf98
Commit
c81bbf98
authored
Aug 16, 2013
by
slene
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
orm now use a filed named `Id` as default auto primary key, you can ignore `orm:"auto"` setting
parent
f02b286a
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
44 additions
and
18 deletions
+44
-18
models_boot.go
orm/models_boot.go
+20
-2
models_info_f.go
orm/models_info_f.go
+10
-2
models_test.go
orm/models_test.go
+14
-14
No files found.
orm/models_boot.go
View file @
c81bbf98
...
...
@@ -32,8 +32,26 @@ func registerModel(model interface{}) {
}
if
info
.
fields
.
pk
==
nil
{
fmt
.
Printf
(
"<orm.RegisterModel> `%s` need a primary key field
\n
"
,
name
)
os
.
Exit
(
2
)
outFor
:
for
_
,
fi
:=
range
info
.
fields
.
fieldsDB
{
if
fi
.
name
==
"Id"
{
if
fi
.
sf
.
Tag
.
Get
(
defaultStructTagName
)
==
""
{
switch
fi
.
addrValue
.
Elem
()
.
Kind
()
{
case
reflect
.
Int
,
reflect
.
Int64
,
reflect
.
Int32
:
fi
.
auto
=
true
fi
.
pk
=
true
info
.
fields
.
pk
=
fi
break
outFor
}
}
}
}
if
info
.
fields
.
pk
==
nil
{
fmt
.
Printf
(
"<orm.RegisterModel> `%s` need a primary key field
\n
"
,
name
)
os
.
Exit
(
2
)
}
}
info
.
table
=
table
...
...
orm/models_info_f.go
View file @
c81bbf98
...
...
@@ -89,7 +89,7 @@ type fieldInfo struct {
fullName
string
column
string
addrValue
reflect
.
Value
sf
*
reflect
.
StructField
sf
reflect
.
StructField
auto
bool
pk
bool
null
bool
...
...
@@ -244,7 +244,7 @@ checkType:
fi
.
name
=
sf
.
Name
fi
.
column
=
getColumnName
(
fieldType
,
addrField
,
sf
,
tags
[
"column"
])
fi
.
addrValue
=
addrField
fi
.
sf
=
&
sf
fi
.
sf
=
sf
fi
.
fullName
=
mi
.
fullName
+
"."
+
sf
.
Name
fi
.
null
=
attrs
[
"null"
]
...
...
@@ -358,6 +358,14 @@ checkType:
if
fi
.
auto
||
fi
.
pk
{
if
fi
.
auto
{
switch
addrField
.
Elem
()
.
Kind
()
{
case
reflect
.
Int
,
reflect
.
Int32
,
reflect
.
Int64
:
default
:
err
=
fmt
.
Errorf
(
"auto primary key only support int, int32, int64, but found `%s`"
,
addrField
.
Elem
()
.
Kind
())
goto
end
}
fi
.
pk
=
true
}
fi
.
null
=
false
...
...
orm/models_test.go
View file @
c81bbf98
...
...
@@ -13,10 +13,10 @@ import (
)
type
Data
struct
{
Id
int
`orm:"auto"`
Id
int
Boolean
bool
Char
string
`orm:"size(50)"
`
Text
string
Char
string
`orm:size(50)
`
Text
string
`orm:"type(text)"`
Date
time
.
Time
`orm:"type(date)"`
DateTime
time
.
Time
Byte
byte
...
...
@@ -37,10 +37,10 @@ type Data struct {
}
type
DataNull
struct
{
Id
int
`orm:"auto"`
Id
int
Boolean
bool
`orm:"null"`
Char
string
`orm:"size(50);null"`
Text
string
`orm:"null"`
Text
string
`orm:"
type(text);
null"`
Date
time
.
Time
`orm:"type(date);null"`
DateTime
time
.
Time
`orm:"null"`
Byte
byte
`orm:"null"`
...
...
@@ -61,7 +61,7 @@ type DataNull struct {
}
type
User
struct
{
Id
int
`orm:"auto"`
Id
int
UserName
string
`orm:"size(30);unique"`
Email
string
`orm:"size(100)"`
Password
string
`orm:"size(100)"`
...
...
@@ -81,10 +81,10 @@ func NewUser() *User {
}
type
Profile
struct
{
Id
int
`orm:"auto"`
Age
int16
``
Money
float64
``
User
*
User
`orm:"reverse(one)" json:"-"`
Id
int
Age
int16
Money
float64
User
*
User
`orm:"reverse(one)" json:"-"`
}
func
(
u
*
Profile
)
TableName
()
string
{
...
...
@@ -97,8 +97,8 @@ func NewProfile() *Profile {
}
type
Post
struct
{
Id
int
`orm:"auto"`
User
*
User
`orm:"rel(fk)"`
//
Id
int
User
*
User
`orm:"rel(fk)"`
Title
string
`orm:"size(60)"`
Content
string
``
Created
time
.
Time
`orm:"auto_now_add"`
...
...
@@ -112,7 +112,7 @@ func NewPost() *Post {
}
type
Tag
struct
{
Id
int
`orm:"auto"`
Id
int
Name
string
`orm:"size(30)"`
Posts
[]
*
Post
`orm:"reverse(many)" json:"-"`
}
...
...
@@ -123,7 +123,7 @@ func NewTag() *Tag {
}
type
Comment
struct
{
Id
int
`orm:"auto"`
Id
int
Post
*
Post
`orm:"rel(fk)"`
Content
string
``
Parent
*
Comment
`orm:"null;rel(fk)"`
...
...
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