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
4dbbae61
Commit
4dbbae61
authored
Mar 25, 2016
by
astaxie
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1828 from miraclesu/fix/orm_read_or_create
orm: fix painc when pk is uint on ReadOrCreate
parents
2362ca00
3e2ffa54
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
46 additions
and
7 deletions
+46
-7
db_postgres.go
orm/db_postgres.go
+8
-6
models_test.go
orm/models_test.go
+5
-0
orm.go
orm/orm.go
+8
-1
orm_test.go
orm/orm_test.go
+25
-0
No files found.
orm/db_postgres.go
View file @
4dbbae61
...
...
@@ -123,14 +123,16 @@ func (d *dbBasePostgres) ReplaceMarks(query *string) {
}
// make returning sql support for postgresql.
func
(
d
*
dbBasePostgres
)
HasReturningID
(
mi
*
modelInfo
,
query
*
string
)
(
has
bool
)
{
if
mi
.
fields
.
pk
.
auto
{
if
query
!=
nil
{
*
query
=
fmt
.
Sprintf
(
`%s RETURNING "%s"`
,
*
query
,
mi
.
fields
.
pk
.
column
)
func
(
d
*
dbBasePostgres
)
HasReturningID
(
mi
*
modelInfo
,
query
*
string
)
bool
{
fi
:=
mi
.
fields
.
pk
if
fi
.
fieldType
&
IsPositiveIntegerField
==
0
&&
fi
.
fieldType
&
IsIntegerField
==
0
{
return
false
}
has
=
true
if
query
!=
nil
{
*
query
=
fmt
.
Sprintf
(
`%s RETURNING "%s"`
,
*
query
,
fi
.
column
)
}
return
return
true
}
// show table sql for postgresql.
...
...
orm/models_test.go
View file @
4dbbae61
...
...
@@ -392,6 +392,11 @@ type IntegerPk struct {
Value
string
}
type
UintPk
struct
{
Id
uint32
`orm:"pk"`
Name
string
}
var
DBARGS
=
struct
{
Driver
string
Source
string
...
...
orm/orm.go
View file @
4dbbae61
...
...
@@ -140,7 +140,14 @@ func (o *orm) ReadOrCreate(md interface{}, col1 string, cols ...string) (bool, i
return
(
err
==
nil
),
id
,
err
}
return
false
,
ind
.
FieldByIndex
(
mi
.
fields
.
pk
.
fieldIndex
)
.
Int
(),
err
id
,
vid
:=
int64
(
0
),
ind
.
FieldByIndex
(
mi
.
fields
.
pk
.
fieldIndex
)
if
mi
.
fields
.
pk
.
fieldType
&
IsPositiveIntegerField
>
0
{
id
=
int64
(
vid
.
Uint
())
}
else
{
id
=
vid
.
Int
()
}
return
false
,
id
,
err
}
// insert model data to database
...
...
orm/orm_test.go
View file @
4dbbae61
...
...
@@ -191,6 +191,7 @@ func TestSyncDb(t *testing.T) {
RegisterModel
(
new
(
InLine
))
RegisterModel
(
new
(
InLineOneToOne
))
RegisterModel
(
new
(
IntegerPk
))
RegisterModel
(
new
(
UintPk
))
err
:=
RunSyncdb
(
"default"
,
true
,
Debug
)
throwFail
(
t
,
err
)
...
...
@@ -213,6 +214,7 @@ func TestRegisterModels(t *testing.T) {
RegisterModel
(
new
(
InLine
))
RegisterModel
(
new
(
InLineOneToOne
))
RegisterModel
(
new
(
IntegerPk
))
RegisterModel
(
new
(
UintPk
))
BootStrap
()
...
...
@@ -2013,3 +2015,26 @@ func TestIntegerPk(t *testing.T) {
throwFail
(
t
,
AssertIs
(
out
.
Value
,
intPk
.
Value
))
}
}
func
TestUintPk
(
t
*
testing
.
T
)
{
name
:=
"go"
u
:=
&
UintPk
{
Id
:
8
,
Name
:
name
,
}
created
,
pk
,
err
:=
dORM
.
ReadOrCreate
(
u
,
"Id"
)
throwFail
(
t
,
err
)
throwFail
(
t
,
AssertIs
(
created
,
true
))
throwFail
(
t
,
AssertIs
(
u
.
Name
,
name
))
nu
:=
&
UintPk
{
Id
:
8
}
created
,
pk
,
err
=
dORM
.
ReadOrCreate
(
nu
,
"Id"
)
throwFail
(
t
,
err
)
throwFail
(
t
,
AssertIs
(
created
,
false
))
throwFail
(
t
,
AssertIs
(
nu
.
Id
,
u
.
Id
))
throwFail
(
t
,
AssertIs
(
pk
,
u
.
Id
))
throwFail
(
t
,
AssertIs
(
nu
.
Name
,
name
))
dORM
.
Delete
(
u
)
}
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