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 3fbd5316
authored
Jun 01, 2018
by
Scott BonAmi
Committed by
Sushant
Jun 01, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix(create): allow foreign key to be null when association reference is set (#9466)
1 parent
bce3d9aa
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
37 additions
and
0 deletions
lib/instance-validator.js
test/integration/model/create/include.test.js
lib/instance-validator.js
View file @
3fbd531
...
@@ -6,6 +6,7 @@ const Utils = require('./utils');
...
@@ -6,6 +6,7 @@ const Utils = require('./utils');
const
sequelizeError
=
require
(
'./errors'
);
const
sequelizeError
=
require
(
'./errors'
);
const
Promise
=
require
(
'./promise'
);
const
Promise
=
require
(
'./promise'
);
const
DataTypes
=
require
(
'./data-types'
);
const
DataTypes
=
require
(
'./data-types'
);
const
BelongsTo
=
require
(
'./associations/belongs-to'
);
const
_
=
require
(
'lodash'
);
const
_
=
require
(
'lodash'
);
/**
/**
...
@@ -320,6 +321,8 @@ class InstanceValidator {
...
@@ -320,6 +321,8 @@ class InstanceValidator {
*/
*/
_validateSchema
(
rawAttribute
,
field
,
value
)
{
_validateSchema
(
rawAttribute
,
field
,
value
)
{
if
(
rawAttribute
.
allowNull
===
false
&&
(
value
===
null
||
value
===
undefined
))
{
if
(
rawAttribute
.
allowNull
===
false
&&
(
value
===
null
||
value
===
undefined
))
{
const
association
=
_
.
values
(
this
.
modelInstance
.
constructor
.
associations
).
find
(
association
=>
association
instanceof
BelongsTo
&&
association
.
foreignKey
===
rawAttribute
.
fieldName
);
if
(
!
association
||
!
this
.
modelInstance
.
get
(
association
.
associationAccessor
))
{
const
validators
=
this
.
modelInstance
.
validators
[
field
];
const
validators
=
this
.
modelInstance
.
validators
[
field
];
const
errMsg
=
_
.
get
(
validators
,
'notNull.msg'
,
`
${
this
.
modelInstance
.
constructor
.
name
}
.
${
field
}
cannot be null`
);
const
errMsg
=
_
.
get
(
validators
,
'notNull.msg'
,
`
${
this
.
modelInstance
.
constructor
.
name
}
.
${
field
}
cannot be null`
);
...
@@ -332,6 +335,7 @@ class InstanceValidator {
...
@@ -332,6 +335,7 @@ class InstanceValidator {
'is_null'
'is_null'
));
));
}
}
}
if
(
rawAttribute
.
type
===
DataTypes
.
STRING
||
rawAttribute
.
type
instanceof
DataTypes
.
STRING
||
rawAttribute
.
type
===
DataTypes
.
TEXT
||
rawAttribute
.
type
instanceof
DataTypes
.
TEXT
)
{
if
(
rawAttribute
.
type
===
DataTypes
.
STRING
||
rawAttribute
.
type
instanceof
DataTypes
.
STRING
||
rawAttribute
.
type
===
DataTypes
.
TEXT
||
rawAttribute
.
type
instanceof
DataTypes
.
TEXT
)
{
if
(
Array
.
isArray
(
value
)
||
_
.
isObject
(
value
)
&&
!
(
value
instanceof
Utils
.
SequelizeMethod
)
&&
!
Buffer
.
isBuffer
(
value
))
{
if
(
Array
.
isArray
(
value
)
||
_
.
isObject
(
value
)
&&
!
(
value
instanceof
Utils
.
SequelizeMethod
)
&&
!
Buffer
.
isBuffer
(
value
))
{
...
...
test/integration/model/create/include.test.js
View file @
3fbd531
...
@@ -60,6 +60,39 @@ describe(Support.getTestDialectTeaser('Model'), () => {
...
@@ -60,6 +60,39 @@ describe(Support.getTestDialectTeaser('Model'), () => {
});
});
});
});
it
(
'should create data for BelongsTo relations with no nullable FK'
,
function
()
{
const
Product
=
this
.
sequelize
.
define
(
'Product'
,
{
title
:
Sequelize
.
STRING
});
const
User
=
this
.
sequelize
.
define
(
'User'
,
{
first_name
:
Sequelize
.
STRING
});
Product
.
belongsTo
(
User
,
{
foreignKey
:
{
allowNull
:
false
}
});
return
this
.
sequelize
.
sync
({
force
:
true
}).
then
(()
=>
{
return
Product
.
create
({
title
:
'Chair'
,
User
:
{
first_name
:
'Mick'
}
},
{
include
:
[{
model
:
User
}]
}).
then
(
savedProduct
=>
{
expect
(
savedProduct
).
to
.
exist
;
expect
(
savedProduct
.
title
).
to
.
be
.
equal
(
'Chair'
);
expect
(
savedProduct
.
User
).
to
.
exist
;
expect
(
savedProduct
.
User
.
first_name
).
to
.
be
.
equal
(
'Mick'
);
});
});
});
it
(
'should create data for BelongsTo relations with alias'
,
function
()
{
it
(
'should create data for BelongsTo relations with alias'
,
function
()
{
const
Product
=
this
.
sequelize
.
define
(
'Product'
,
{
const
Product
=
this
.
sequelize
.
define
(
'Product'
,
{
title
:
Sequelize
.
STRING
title
:
Sequelize
.
STRING
...
...
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