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 efa9fe82
authored
Apr 12, 2015
by
Mick Hansen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix(model): default values should be used if value is undefined, closes #3288
1 parent
b9d42fc9
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
47 additions
and
28 deletions
lib/instance.js
test/integration/model/create.test.js
test/unit/instance/build.test.js
lib/instance.js
View file @
efa9fe8
...
@@ -1070,7 +1070,7 @@ module.exports = (function() {
...
@@ -1070,7 +1070,7 @@ module.exports = (function() {
if
(
this
.
Model
.
_hasDefaultValues
)
{
if
(
this
.
Model
.
_hasDefaultValues
)
{
Utils
.
_
.
each
(
this
.
Model
.
_defaultValues
,
function
(
valueFn
,
key
)
{
Utils
.
_
.
each
(
this
.
Model
.
_defaultValues
,
function
(
valueFn
,
key
)
{
if
(
!
defaults
.
hasOwnProperty
(
key
)
)
{
if
(
defaults
[
key
]
===
undefined
)
{
defaults
[
key
]
=
valueFn
();
defaults
[
key
]
=
valueFn
();
}
}
});
});
...
@@ -1100,7 +1100,7 @@ module.exports = (function() {
...
@@ -1100,7 +1100,7 @@ module.exports = (function() {
if
(
Object
.
keys
(
defaults
).
length
)
{
if
(
Object
.
keys
(
defaults
).
length
)
{
for
(
key
in
defaults
)
{
for
(
key
in
defaults
)
{
if
(
!
values
.
hasOwnProperty
(
key
)
)
{
if
(
values
[
key
]
===
undefined
)
{
this
.
set
(
key
,
Utils
.
toDefaultValue
(
defaults
[
key
]),
defaultsOptions
);
this
.
set
(
key
,
Utils
.
toDefaultValue
(
defaults
[
key
]),
defaultsOptions
);
}
}
}
}
...
...
test/integration/model/create.test.js
View file @
efa9fe8
...
@@ -100,6 +100,31 @@ describe(Support.getTestDialectTeaser('Model'), function() {
...
@@ -100,6 +100,31 @@ describe(Support.getTestDialectTeaser('Model'), function() {
});
});
});
});
it
(
'should work with undefined uuid primary key in where'
,
function
()
{
var
User
=
this
.
sequelize
.
define
(
'User'
,
{
id
:
{
type
:
DataTypes
.
UUID
,
primaryKey
:
true
,
allowNull
:
false
,
defaultValue
:
DataTypes
.
UUIDV4
},
name
:
{
type
:
DataTypes
.
STRING
}
});
return
User
.
sync
({
force
:
true
}).
then
(
function
()
{
return
User
.
findOrCreate
({
where
:
{
id
:
undefined
},
defaults
:
{
name
:
Math
.
random
().
toString
()
}
});
});
});
if
([
'sqlite'
,
'mssql'
].
indexOf
(
current
.
dialect
.
name
)
===
-
1
)
{
if
([
'sqlite'
,
'mssql'
].
indexOf
(
current
.
dialect
.
name
)
===
-
1
)
{
it
(
'should not deadlock with no existing entries and no outer transaction'
,
function
()
{
it
(
'should not deadlock with no existing entries and no outer transaction'
,
function
()
{
var
User
=
this
.
sequelize
.
define
(
'User'
,
{
var
User
=
this
.
sequelize
.
define
(
'User'
,
{
...
@@ -457,32 +482,6 @@ describe(Support.getTestDialectTeaser('Model'), function() {
...
@@ -457,32 +482,6 @@ describe(Support.getTestDialectTeaser('Model'), function() {
});
});
});
});
it
(
'works with non-integer primary keys with fields excluding the primary key'
,
function
()
{
var
User
=
this
.
sequelize
.
define
(
'User'
,
{
'id'
:
{
primaryKey
:
true
,
type
:
DataTypes
.
UUID
,
defaultValue
:
DataTypes
.
UUIDV4
,
allowNull
:
false
},
'email'
:
{
type
:
DataTypes
.
STRING
}
});
return
this
.
sequelize
.
sync
({
force
:
true
}).
then
(
function
()
{
return
User
.
create
({
email
:
Math
.
random
().
toString
()
},
{
fields
:
[
'email'
],
logging
:
console
.
log
}).
then
(
function
(
user
)
{
expect
(
user
).
to
.
be
.
ok
;
expect
(
user
.
get
(
'id'
)).
to
.
be
.
ok
;
});
});
});
it
(
'should return an error for a unique constraint error'
,
function
()
{
it
(
'should return an error for a unique constraint error'
,
function
()
{
var
User
=
this
.
sequelize
.
define
(
'User'
,
{
var
User
=
this
.
sequelize
.
define
(
'User'
,
{
'email'
:
{
'email'
:
{
...
...
test/unit/instance/build.test.js
View file @
efa9fe8
...
@@ -31,5 +31,24 @@ describe(Support.getTestDialectTeaser('Instance'), function() {
...
@@ -31,5 +31,24 @@ describe(Support.getTestDialectTeaser('Instance'), function() {
expect
(
instance
.
get
(
'updated_time'
)).
to
.
be
.
ok
;
expect
(
instance
.
get
(
'updated_time'
)).
to
.
be
.
ok
;
expect
(
instance
.
get
(
'updated_time'
)).
to
.
be
.
an
.
instanceof
(
Date
);
expect
(
instance
.
get
(
'updated_time'
)).
to
.
be
.
an
.
instanceof
(
Date
);
});
});
it
(
'should popuplate explicitely undefined UUID primary keys'
,
function
()
{
var
Model
=
current
.
define
(
'Model'
,
{
id
:
{
type
:
DataTypes
.
UUID
,
primaryKey
:
true
,
allowNull
:
false
,
defaultValue
:
DataTypes
.
UUIDV4
}
})
,
instance
;
instance
=
Model
.
build
({
id
:
undefined
});
expect
(
instance
.
get
(
'id'
)).
not
.
to
.
be
.
undefined
;
expect
(
instance
.
get
(
'id'
)).
to
.
be
.
ok
;
});
});
});
});
});
\ No newline at end of file
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