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 c480c35b
authored
Nov 16, 2013
by
Sascha Depold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
transaction support for belongsTo
1 parent
b3bffddd
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
29 additions
and
2 deletions
lib/associations/belongs-to.js
test/associations/belongs-to.test.js
lib/associations/belongs-to.js
View file @
c480c35
...
@@ -70,14 +70,15 @@ module.exports = (function() {
...
@@ -70,14 +70,15 @@ module.exports = (function() {
var
self
=
this
var
self
=
this
,
accessor
=
Utils
.
_
.
camelize
(
'set_'
+
(
this
.
options
.
as
||
Utils
.
singularize
(
this
.
target
.
tableName
,
this
.
target
.
options
.
language
)))
,
accessor
=
Utils
.
_
.
camelize
(
'set_'
+
(
this
.
options
.
as
||
Utils
.
singularize
(
this
.
target
.
tableName
,
this
.
target
.
options
.
language
)))
obj
[
accessor
]
=
function
(
associatedObject
)
{
obj
[
accessor
]
=
function
(
associatedObject
,
options
)
{
var
primaryKeys
=
!!
associatedObject
&&
!!
associatedObject
.
daoFactory
?
Object
.
keys
(
associatedObject
.
daoFactory
.
primaryKeys
)
:
[]
var
primaryKeys
=
!!
associatedObject
&&
!!
associatedObject
.
daoFactory
?
Object
.
keys
(
associatedObject
.
daoFactory
.
primaryKeys
)
:
[]
,
primaryKey
=
primaryKeys
.
length
===
1
?
primaryKeys
[
0
]
:
'id'
,
primaryKey
=
primaryKeys
.
length
===
1
?
primaryKeys
[
0
]
:
'id'
this
[
self
.
identifier
]
=
associatedObject
?
associatedObject
[
primaryKey
]
:
null
this
[
self
.
identifier
]
=
associatedObject
?
associatedObject
[
primaryKey
]
:
null
options
=
Utils
.
_
.
extend
({
fields
:
[
self
.
identifier
],
allowNull
:
[
self
.
identifier
]
},
options
)
// passes the changed field to save, so only that field get updated.
// passes the changed field to save, so only that field get updated.
return
this
.
save
(
[
self
.
identifier
],
{
allowNull
:
[
self
.
identifier
]}
)
return
this
.
save
(
options
)
}
}
return
this
return
this
...
...
test/associations/belongs-to.test.js
View file @
c480c35
...
@@ -21,6 +21,32 @@ describe(Support.getTestDialectTeaser("BelongsTo"), function() {
...
@@ -21,6 +21,32 @@ describe(Support.getTestDialectTeaser("BelongsTo"), function() {
})
})
describe
(
'setAssociation'
,
function
()
{
describe
(
'setAssociation'
,
function
()
{
it
(
'supports transactions'
,
function
(
done
)
{
Support
.
prepareTransactionTest
(
this
.
sequelize
,
function
(
sequelize
)
{
var
User
=
sequelize
.
define
(
'User'
,
{
username
:
Support
.
Sequelize
.
STRING
})
,
Group
=
sequelize
.
define
(
'Group'
,
{
name
:
Support
.
Sequelize
.
STRING
})
Group
.
belongsTo
(
User
)
sequelize
.
sync
({
force
:
true
}).
success
(
function
()
{
User
.
create
({
username
:
'foo'
}).
success
(
function
(
user
)
{
Group
.
create
({
name
:
'bar'
}).
success
(
function
(
group
)
{
sequelize
.
transaction
(
function
(
t
)
{
group
.
setUser
(
user
,
{
transaction
:
t
}).
success
(
function
()
{
Group
.
all
().
success
(
function
(
groups
)
{
groups
[
0
].
getUser
().
success
(
function
(
associatedUser
)
{
expect
(
associatedUser
).
to
.
be
.
null
t
.
rollback
().
success
(
function
()
{
done
()
})
})
})
})
})
})
})
})
})
})
it
(
'can set the association with declared primary keys...'
,
function
(
done
)
{
it
(
'can set the association with declared primary keys...'
,
function
(
done
)
{
var
User
=
this
.
sequelize
.
define
(
'UserXYZ'
,
{
user_id
:
{
type
:
DataTypes
.
INTEGER
,
primaryKey
:
true
},
username
:
DataTypes
.
STRING
})
var
User
=
this
.
sequelize
.
define
(
'UserXYZ'
,
{
user_id
:
{
type
:
DataTypes
.
INTEGER
,
primaryKey
:
true
},
username
:
DataTypes
.
STRING
})
,
Task
=
this
.
sequelize
.
define
(
'TaskXYZ'
,
{
task_id
:
{
type
:
DataTypes
.
INTEGER
,
primaryKey
:
true
},
title
:
DataTypes
.
STRING
})
,
Task
=
this
.
sequelize
.
define
(
'TaskXYZ'
,
{
task_id
:
{
type
:
DataTypes
.
INTEGER
,
primaryKey
:
true
},
title
:
DataTypes
.
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