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 917b5bdb
authored
Mar 28, 2019
by
javiertury
Committed by
Sushant
Mar 28, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix(associations): symmetric getters/setters on foreign keys (#10635)
1 parent
dc6d816d
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
68 additions
and
1 deletions
lib/model.js
test/integration/associations/has-many.test.js
lib/model.js
View file @
917b5bd
...
@@ -3868,7 +3868,7 @@ class Model {
...
@@ -3868,7 +3868,7 @@ class Model {
return
include
.
association
.
throughModel
.
create
(
values
,
includeOptions
);
return
include
.
association
.
throughModel
.
create
(
values
,
includeOptions
);
});
});
}
}
instance
.
set
(
include
.
association
.
foreignKey
,
this
.
get
(
include
.
association
.
sourceKey
||
this
.
constructor
.
primaryKeyAttribute
,
{
raw
:
true
}));
instance
.
set
(
include
.
association
.
foreignKey
,
this
.
get
(
include
.
association
.
sourceKey
||
this
.
constructor
.
primaryKeyAttribute
,
{
raw
:
true
})
,
{
raw
:
true
}
);
Object
.
assign
(
instance
,
include
.
association
.
scope
);
Object
.
assign
(
instance
,
include
.
association
.
scope
);
return
instance
.
save
(
includeOptions
);
return
instance
.
save
(
includeOptions
);
});
});
...
...
test/integration/associations/has-many.test.js
View file @
917b5bd
...
@@ -1590,6 +1590,73 @@ describe(Support.getTestDialectTeaser('HasMany'), () => {
...
@@ -1590,6 +1590,73 @@ describe(Support.getTestDialectTeaser('HasMany'), () => {
expect
(
tasks
[
0
].
title
).
to
.
be
.
equal
(
values
.
tasks
[
0
].
title
,
'task title is correct'
);
expect
(
tasks
[
0
].
title
).
to
.
be
.
equal
(
values
.
tasks
[
0
].
title
,
'task title is correct'
);
}));
}));
});
});
it
(
'should create nested associations with symmetric getters/setters on FK'
,
function
()
{
// Dummy getter/setter to test they are symmetric
function
toCustomFormat
(
string
)
{
return
string
&&
`FORMAT-
${
string
}
`
;
}
function
fromCustomFormat
(
string
)
{
return
string
&&
string
.
slice
(
7
);
}
const
Parent
=
this
.
sequelize
.
define
(
'Parent'
,
{
id
:
{
type
:
DataTypes
.
STRING
,
primaryKey
:
true
,
get
()
{
return
fromCustomFormat
(
this
.
getDataValue
(
'id'
));
},
set
(
value
)
{
this
.
setDataValue
(
'id'
,
toCustomFormat
(
value
));
}
}
});
const
Child
=
this
.
sequelize
.
define
(
'Child'
,
{
id
:
{
type
:
DataTypes
.
STRING
,
primaryKey
:
true
},
parent
:
{
type
:
DataTypes
.
STRING
,
get
()
{
return
fromCustomFormat
(
this
.
getDataValue
(
'parent'
));
},
set
(
value
)
{
this
.
setDataValue
(
'parent'
,
toCustomFormat
(
value
));
}
}
});
Child
.
belongsTo
(
Parent
,
{
foreignKey
:
'parent'
,
targetKey
:
'id'
});
Parent
.
hasMany
(
Child
,
{
foreignKey
:
'parent'
,
targetKey
:
'id'
,
as
:
'children'
});
const
values
=
{
id
:
'sJn369d8Em'
,
children
:
[{
id
:
'dgeQAQaW7A'
}]
};
return
this
.
sequelize
.
sync
({
force
:
true
})
.
then
(()
=>
Parent
.
create
(
values
,
{
include
:
{
model
:
Child
,
as
:
'children'
}
}))
.
then
(
father
=>
{
// Make sure tasks are defined for created user
expect
(
father
.
id
).
to
.
be
.
equal
(
'sJn369d8Em'
);
expect
(
father
.
get
(
'id'
,
{
raw
:
true
})).
to
.
be
.
equal
(
'FORMAT-sJn369d8Em'
);
expect
(
father
).
to
.
have
.
property
(
'children'
);
expect
(
father
.
children
).
to
.
be
.
an
(
'array'
);
expect
(
father
.
children
).
to
.
lengthOf
(
1
);
expect
(
father
.
children
[
0
].
parent
).
to
.
be
.
equal
(
'sJn369d8Em'
);
expect
(
father
.
children
[
0
].
get
(
'parent'
,
{
raw
:
true
})).
to
.
be
.
equal
(
'FORMAT-sJn369d8Em'
);
});
});
});
});
describe
(
'sourceKey with where clause in include'
,
()
=>
{
describe
(
'sourceKey with where clause in include'
,
()
=>
{
...
...
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