Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
public
/
sequelize
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
不要怂,就是干,撸起袖子干!
Commit dbf28085
authored
May 14, 2011
by
Allan Carroll
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Set insertId for non-default auto increment fields
1 parent
46cfe2c4
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
38 additions
and
3 deletions
lib/sequelize/model-definition.js
lib/sequelize/query.js
test/Model/create.js
test/Sequelize/define.js
test/config.js
lib/sequelize/model-definition.js
View file @
dbf2808
...
@@ -17,6 +17,7 @@ var ModelDefinition = module.exports = function(name, attributes, options) {
...
@@ -17,6 +17,7 @@ var ModelDefinition = module.exports = function(name, attributes, options) {
this
.
addDefaultAttributes
()
this
.
addDefaultAttributes
()
this
.
addOptionalClassMethods
()
this
.
addOptionalClassMethods
()
this
.
findAutoIncrementField
()
}
}
Utils
.
addEventEmitter
(
ModelDefinition
)
Utils
.
addEventEmitter
(
ModelDefinition
)
...
@@ -43,6 +44,19 @@ ModelDefinition.prototype.addDefaultAttributes = function() {
...
@@ -43,6 +44,19 @@ ModelDefinition.prototype.addDefaultAttributes = function() {
Utils
.
_
.
map
(
defaultAttributes
,
function
(
value
,
attr
)
{
self
.
attributes
[
attr
]
=
value
})
Utils
.
_
.
map
(
defaultAttributes
,
function
(
value
,
attr
)
{
self
.
attributes
[
attr
]
=
value
})
}
}
ModelDefinition
.
prototype
.
findAutoIncrementField
=
function
()
{
var
self
=
this
;
this
.
autoIncrementField
=
null
;
Utils
.
_
.
map
(
this
.
attributes
,
function
(
definition
,
name
)
{
if
(
definition
&&
definition
.
indexOf
(
'auto_increment'
)
>=
0
)
{
if
(
self
.
autoIncrementField
)
{
throw
new
Error
(
'Invalid model definition. Only one autoincrement field allowed.'
)
}
self
.
autoIncrementField
=
name
;
}
})
}
ModelDefinition
.
prototype
.
query
=
function
()
{
ModelDefinition
.
prototype
.
query
=
function
()
{
var
args
=
Utils
.
_
.
map
(
arguments
,
function
(
arg
,
_
)
{
return
arg
})
var
args
=
Utils
.
_
.
map
(
arguments
,
function
(
arg
,
_
)
{
return
arg
})
,
s
=
this
.
modelManager
.
sequelize
,
s
=
this
.
modelManager
.
sequelize
...
...
lib/sequelize/query.js
View file @
dbf2808
...
@@ -34,8 +34,8 @@ Query.prototype.onSuccess = function(query, results, fields) {
...
@@ -34,8 +34,8 @@ Query.prototype.onSuccess = function(query, results, fields) {
,
self
=
this
,
self
=
this
// add the inserted row id to the instance
// add the inserted row id to the instance
if
(
this
.
callee
&&
!
this
.
callee
.
options
.
hasPrimaryKeys
&&
(
query
.
indexOf
(
'INSERT INTO'
)
==
0
)
&&
(
results
.
hasOwnProperty
(
'insertId'
)))
if
(
this
.
callee
&&
(
query
.
indexOf
(
'INSERT INTO'
)
==
0
)
&&
(
results
.
hasOwnProperty
(
'insertId'
)))
this
.
callee
.
id
=
results
.
insertId
this
.
callee
[
this
.
callee
.
definition
.
autoIncrementField
]
=
results
.
insertId
// transform results into real model instances
// transform results into real model instances
// return the first real model instance if options.plain is set (e.g. Model.find)
// return the first real model instance if options.plain is set (e.g. Model.find)
...
...
test/Model/create.js
View file @
dbf2808
...
@@ -37,5 +37,16 @@ module.exports = {
...
@@ -37,5 +37,16 @@ module.exports = {
})
})
})
})
})
})
},
'it should set the auto increment field to the insert id'
:
function
(
exit
)
{
var
User
=
sequelize
.
define
(
'User'
+
config
.
rand
(),
{
userid
:
{
type
:
Sequelize
.
INTEGER
,
autoIncrement
:
true
,
primaryKey
:
true
,
allowNull
:
false
}
})
User
.
sync
({
force
:
true
}).
on
(
'success'
,
function
()
{
User
.
create
({}).
on
(
'success'
,
function
(
user
)
{
assert
.
eql
(
user
.
userid
,
1
)
exit
(
function
(){})
})
})
}
}
}
}
\ No newline at end of file
test/Sequelize/define.js
View file @
dbf2808
...
@@ -75,5 +75,13 @@ module.exports = {
...
@@ -75,5 +75,13 @@ module.exports = {
assert
.
isDefined
(
User
.
build
().
makeItSo
)
assert
.
isDefined
(
User
.
build
().
makeItSo
)
assert
.
eql
(
User
.
build
().
makeItSo
(),
2
)
assert
.
eql
(
User
.
build
().
makeItSo
(),
2
)
},
'it shouldn\'t allow two auto increment fields'
:
function
()
{
assert
.
throws
(
function
()
{
var
User
=
sequelize
.
define
(
'User'
,
{
userid
:
{
type
:
Sequelize
.
INTEGER
,
primaryKey
:
true
,
autoIncrement
:
true
},
userscore
:
{
type
:
Sequelize
.
INTEGER
,
primaryKey
:
true
,
autoIncrement
:
true
},
})
})
}
}
}
}
\ No newline at end of file
test/config.js
View file @
dbf2808
module
.
exports
=
{
module
.
exports
=
{
username
:
'root'
username
:
'root'
,
password
:
null
,
password
:
'password'
,
database
:
'sequelize_test'
,
database
:
'sequelize_test'
,
host
:
'127.0.0.1'
,
host
:
'127.0.0.1'
,
rand
:
function
()
{
,
rand
:
function
()
{
...
...
Write
Preview
Markdown
is supported
Attach a file
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 post a comment