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 0fb0f54e
authored
Apr 12, 2011
by
Sascha Depold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
dont add id as primary key if primaryKeys were defined for the model
1 parent
25491005
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
26 additions
and
1 deletions
lib/sequelize/model-definition.js
test/Sequelize/define.js
lib/sequelize/model-definition.js
View file @
0fb0f54
...
@@ -21,6 +21,9 @@ ModelDefinition.prototype.addDefaultAttributes = function() {
...
@@ -21,6 +21,9 @@ ModelDefinition.prototype.addDefaultAttributes = function() {
var
defaultAttributes
=
{
id
:
{
type
:
DataTypes
.
INTEGER
,
allowNull
:
false
,
primaryKey
:
true
,
autoIncrement
:
true
}}
var
defaultAttributes
=
{
id
:
{
type
:
DataTypes
.
INTEGER
,
allowNull
:
false
,
primaryKey
:
true
,
autoIncrement
:
true
}}
,
self
=
this
,
self
=
this
if
(
Utils
.
_
.
keys
(
this
.
primaryKeys
).
length
>
0
)
defaultAttributes
=
{}
if
(
this
.
options
.
timestamps
)
{
if
(
this
.
options
.
timestamps
)
{
defaultAttributes
[
this
.
options
.
camelcase
?
'created_at'
:
'createdAt'
]
=
{
type
:
DataTypes
.
DATE
,
allowNull
:
false
}
defaultAttributes
[
this
.
options
.
camelcase
?
'created_at'
:
'createdAt'
]
=
{
type
:
DataTypes
.
DATE
,
allowNull
:
false
}
defaultAttributes
[
this
.
options
.
camelcase
?
'updated_at'
:
'updatedAt'
]
=
{
type
:
DataTypes
.
DATE
,
allowNull
:
false
}
defaultAttributes
[
this
.
options
.
camelcase
?
'updated_at'
:
'updatedAt'
]
=
{
type
:
DataTypes
.
DATE
,
allowNull
:
false
}
...
@@ -74,6 +77,9 @@ ModelDefinition.prototype.find = function(options) {
...
@@ -74,6 +77,9 @@ ModelDefinition.prototype.find = function(options) {
// options is not a hash but an id
// options is not a hash but an id
if
(
typeof
options
==
'number'
)
if
(
typeof
options
==
'number'
)
options
=
{
where
:
options
}
options
=
{
where
:
options
}
else
{
// if(arguments.length == this.primaryKeys.length)
}
options
.
limit
=
1
options
.
limit
=
1
...
@@ -94,4 +100,15 @@ ModelDefinition.prototype.create = function(values) {
...
@@ -94,4 +100,15 @@ ModelDefinition.prototype.create = function(values) {
return
this
.
build
(
values
).
save
()
return
this
.
build
(
values
).
save
()
}
}
ModelDefinition
.
prototype
.
__defineGetter__
(
'primaryKeys'
,
function
()
{
var
result
=
{}
Utils
.
_
.
each
(
this
.
attributes
,
function
(
dataTypeString
,
attributeName
)
{
if
(
dataTypeString
.
indexOf
(
'PRIMARY KEY'
)
>
-
1
)
result
[
attributeName
]
=
dataTypeString
})
return
result
})
Utils
.
_
.
map
(
require
(
"./association-mixin"
).
classMethods
,
function
(
fct
,
name
)
{
ModelDefinition
.
prototype
[
name
]
=
fct
})
Utils
.
_
.
map
(
require
(
"./association-mixin"
).
classMethods
,
function
(
fct
,
name
)
{
ModelDefinition
.
prototype
[
name
]
=
fct
})
\ No newline at end of file
test/Sequelize/define.js
View file @
0fb0f54
...
@@ -31,7 +31,14 @@ module.exports = {
...
@@ -31,7 +31,14 @@ module.exports = {
var
User
=
sequelize
.
define
(
'User'
+
parseInt
(
Math
.
random
()
*
999999999
),
{
var
User
=
sequelize
.
define
(
'User'
+
parseInt
(
Math
.
random
()
*
999999999
),
{
username
:
{
type
:
Sequelize
.
STRING
,
primaryKey
:
true
}
username
:
{
type
:
Sequelize
.
STRING
,
primaryKey
:
true
}
},
{
timestamps
:
false
})
},
{
timestamps
:
false
})
assert
.
eql
(
User
.
attributes
,
{
username
:
"VARCHAR(255) PRIMARY KEY"
,
id
:
"INT NOT NULL auto_increment PRIMARY KEY"
})
assert
.
eql
(
User
.
attributes
,
{
username
:
"VARCHAR(255) PRIMARY KEY"
})
},
'primaryKeys should be correctly determined'
:
function
()
{
var
User
=
sequelize
.
define
(
'User'
+
parseInt
(
Math
.
random
()
*
999999999
),
{
foo
:
{
type
:
Sequelize
.
STRING
,
primaryKey
:
true
},
bar
:
Sequelize
.
STRING
})
assert
.
eql
(
User
.
primaryKeys
,
{
"foo"
:
"VARCHAR(255) PRIMARY KEY"
})
},
},
'it should add updatedAt and createdAt if timestamps is undefined or true'
:
function
()
{
'it should add updatedAt and createdAt if timestamps is undefined or true'
:
function
()
{
var
User1
=
sequelize
.
define
(
'User'
+
parseInt
(
Math
.
random
()
*
999999999
),
{})
var
User1
=
sequelize
.
define
(
'User'
+
parseInt
(
Math
.
random
()
*
999999999
),
{})
...
...
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