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 931cfaad
authored
Jan 02, 2014
by
Mick Hansen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Test and fix for ordering with belongsTo includes
1 parent
6df7d196
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
90 additions
and
76 deletions
lib/associations/belongs-to.js
lib/dao-factory.js
lib/dialects/abstract/query-generator.js
test/include.test.js
lib/associations/belongs-to.js
View file @
931cfaa
...
@@ -10,24 +10,25 @@ module.exports = (function() {
...
@@ -10,24 +10,25 @@ module.exports = (function() {
this
.
options
=
options
this
.
options
=
options
this
.
isSingleAssociation
=
true
this
.
isSingleAssociation
=
true
this
.
isSelfAssociation
=
(
this
.
source
.
tableName
==
this
.
target
.
tableName
)
this
.
isSelfAssociation
=
(
this
.
source
.
tableName
==
this
.
target
.
tableName
)
this
.
as
=
this
.
options
.
as
if
(
this
.
isSelfAssociation
&&
!
this
.
options
.
foreignKey
&&
!!
this
.
options
.
as
)
{
if
(
this
.
isSelfAssociation
&&
!
this
.
options
.
foreignKey
&&
!!
this
.
as
)
{
this
.
options
.
foreignKey
=
Utils
.
_
.
underscoredIf
(
Utils
.
singularize
(
this
.
options
.
as
,
this
.
source
.
options
.
language
)
+
"Id"
,
this
.
source
.
options
.
underscored
)
this
.
options
.
foreignKey
=
Utils
.
_
.
underscoredIf
(
Utils
.
singularize
(
this
.
as
,
this
.
source
.
options
.
language
)
+
"Id"
,
this
.
source
.
options
.
underscored
)
}
}
if
(
!
this
.
options
.
as
)
{
if
(
!
this
.
as
)
{
this
.
options
.
as
=
Utils
.
singularize
(
this
.
target
.
tableName
,
this
.
target
.
options
.
language
)
this
.
as
=
this
.
options
.
as
=
Utils
.
singularize
(
this
.
target
.
tableName
,
this
.
target
.
options
.
language
)
}
}
this
.
associationAccessor
=
this
.
isSelfAssociation
this
.
associationAccessor
=
this
.
isSelfAssociation
?
Utils
.
combineTableNames
(
this
.
target
.
tableName
,
this
.
options
.
as
)
?
Utils
.
combineTableNames
(
this
.
target
.
tableName
,
this
.
as
)
:
this
.
options
.
as
:
this
.
as
this
.
options
.
useHooks
=
options
.
useHooks
this
.
options
.
useHooks
=
options
.
useHooks
this
.
accessors
=
{
this
.
accessors
=
{
get
:
Utils
.
_
.
camelize
(
'get_'
+
this
.
options
.
as
),
get
:
Utils
.
_
.
camelize
(
'get_'
+
this
.
as
),
set
:
Utils
.
_
.
camelize
(
'set_'
+
this
.
options
.
as
)
set
:
Utils
.
_
.
camelize
(
'set_'
+
this
.
as
)
}
}
}
}
...
@@ -37,7 +38,7 @@ module.exports = (function() {
...
@@ -37,7 +38,7 @@ module.exports = (function() {
,
targetKeys
=
Object
.
keys
(
this
.
target
.
primaryKeys
)
,
targetKeys
=
Object
.
keys
(
this
.
target
.
primaryKeys
)
,
keyType
=
((
this
.
target
.
hasPrimaryKeys
&&
targetKeys
.
length
===
1
)
?
this
.
target
.
rawAttributes
[
targetKeys
[
0
]].
type
:
DataTypes
.
INTEGER
)
,
keyType
=
((
this
.
target
.
hasPrimaryKeys
&&
targetKeys
.
length
===
1
)
?
this
.
target
.
rawAttributes
[
targetKeys
[
0
]].
type
:
DataTypes
.
INTEGER
)
this
.
identifier
=
this
.
options
.
foreignKey
||
Utils
.
_
.
underscoredIf
(
Utils
.
singularize
(
this
.
target
.
tableName
,
this
.
target
.
options
.
language
)
+
"Id"
,
this
.
source
.
options
.
underscored
)
this
.
identifier
=
this
.
options
.
foreignKey
||
Utils
.
_
.
underscoredIf
(
this
.
as
+
"Id"
,
this
.
source
.
options
.
underscored
)
newAttributes
[
this
.
identifier
]
=
{
type
:
this
.
options
.
keyType
||
keyType
}
newAttributes
[
this
.
identifier
]
=
{
type
:
this
.
options
.
keyType
||
keyType
}
Helpers
.
addForeignKeyConstraints
(
newAttributes
[
this
.
identifier
],
this
.
target
,
this
.
source
,
this
.
options
)
Helpers
.
addForeignKeyConstraints
(
newAttributes
[
this
.
identifier
],
this
.
target
,
this
.
source
,
this
.
options
)
Utils
.
_
.
defaults
(
this
.
source
.
rawAttributes
,
newAttributes
)
Utils
.
_
.
defaults
(
this
.
source
.
rawAttributes
,
newAttributes
)
...
...
lib/dao-factory.js
View file @
931cfaa
...
@@ -1199,11 +1199,16 @@ module.exports = (function() {
...
@@ -1199,11 +1199,16 @@ module.exports = (function() {
var
validateIncludedElements
=
function
(
options
)
{
var
validateIncludedElements
=
function
(
options
)
{
options
.
includeNames
=
[]
options
.
includeNames
=
[]
options
.
includeMap
=
{}
options
.
includeMap
=
{}
options
.
hasSingleAssociation
=
false
options
.
hasMultiAssociation
=
false
options
.
include
=
options
.
include
.
map
(
function
(
include
)
{
options
.
include
=
options
.
include
.
map
(
function
(
include
)
{
include
=
validateIncludedElement
.
call
(
this
,
include
,
options
.
daoFactory
)
include
=
validateIncludedElement
.
call
(
this
,
include
,
options
.
daoFactory
)
if
(
include
.
association
.
isMultiAssociation
)
options
.
hasMultiAssociation
=
true
if
(
include
.
association
.
isSingleAssociation
)
options
.
hasSingleAssociation
=
true
options
.
includeMap
[
include
.
as
]
=
include
options
.
includeMap
[
include
.
as
]
=
include
options
.
includeNames
.
push
(
include
.
as
)
options
.
includeNames
.
push
(
include
.
as
)
return
include
return
include
}.
bind
(
this
))
}.
bind
(
this
))
};
};
...
...
lib/dialects/abstract/query-generator.js
View file @
931cfaa
...
@@ -437,7 +437,7 @@ module.exports = (function() {
...
@@ -437,7 +437,7 @@ module.exports = (function() {
options
.
attributes
=
optAttributes
.
join
(
', '
)
options
.
attributes
=
optAttributes
.
join
(
', '
)
}
}
var
conditionalJoins
=
this
.
getConditionalJoins
(
options
,
factory
),
var
conditionalJoins
=
options
.
hasMultiAssociation
&&
this
.
getConditionalJoins
(
options
,
factory
),
query
;
query
;
if
(
conditionalJoins
)
{
if
(
conditionalJoins
)
{
...
@@ -626,12 +626,14 @@ module.exports = (function() {
...
@@ -626,12 +626,14 @@ module.exports = (function() {
var
associationParts
=
filterStr
.
split
(
'.'
)
var
associationParts
=
filterStr
.
split
(
'.'
)
,
attributePart
=
associationParts
.
pop
()
,
attributePart
=
associationParts
.
pop
()
,
self
=
this
,
self
=
this
,
association
associationParts
.
forEach
(
function
(
attribute
)
{
associationParts
.
forEach
(
function
(
attribute
)
{
association
=
self
.
findAssociation
(
attribute
,
dao
)
dao
=
self
.
findAssociation
(
attribute
,
dao
).
target
;
dao
=
self
.
findAssociation
(
attribute
,
dao
).
target
;
})
})
return
dao
.
tableName
+
'.'
+
attributePart
;
return
(
association
.
as
||
association
.
options
.
as
||
dao
.
tableName
)
+
'.'
+
attributePart
;
},
},
getConditionalJoins
:
function
(
options
,
originalDao
){
getConditionalJoins
:
function
(
options
,
originalDao
){
...
...
test/include.test.js
View file @
931cfaa
...
@@ -743,84 +743,90 @@ describe(Support.getTestDialectTeaser("Include"), function () {
...
@@ -743,84 +743,90 @@ describe(Support.getTestDialectTeaser("Include"), function () {
})
})
})
})
xit
(
'should support the order attribute on multiple associated entiti
es'
,
function
(
done
)
{
it
(
'should support ordering with only belongsTo includ
es'
,
function
(
done
)
{
var
User
=
this
.
sequelize
.
define
(
'User'
,
{})
var
User
=
this
.
sequelize
.
define
(
'User'
,
{})
,
Item
=
this
.
sequelize
.
define
(
'Item'
,
{
'test'
:
DataTypes
.
STRING
})
,
Item
=
this
.
sequelize
.
define
(
'Item'
,
{
'test'
:
DataTypes
.
STRING
})
,
Order
=
this
.
sequelize
.
define
(
'Order'
,
{
'position'
:
DataTypes
.
INTEGER
})
,
Order
=
this
.
sequelize
.
define
(
'Order'
,
{
'position'
:
DataTypes
.
INTEGER
})
User
.
belongsTo
(
Item
,
{
'as'
:
'itemA'
})
User
.
belongsTo
(
Item
,
{
'as'
:
'itemA'
})
User
.
belongsTo
(
Item
,
{
'as'
:
'itemB'
})
User
.
belongsTo
(
Item
,
{
'as'
:
'itemB'
})
User
.
belongsTo
(
Order
)
User
.
belongsTo
(
Order
)
Item
.
hasMany
(
User
)
Order
.
hasMany
(
User
)
this
.
sequelize
.
sync
().
done
(
function
()
{
async
.
auto
({
this
.
sequelize
.
sync
().
done
(
function
()
{
users
:
function
(
callback
)
{
async
.
auto
({
User
.
bulkCreate
([{},
{},
{}]).
done
(
function
()
{
users
:
function
(
callback
)
{
User
.
findAll
().
done
(
callback
)
User
.
bulkCreate
([{},
{},
{}]).
done
(
function
()
{
})
User
.
findAll
().
done
(
callback
)
},
})
items
:
function
(
callback
)
{
},
Item
.
bulkCreate
([
items
:
function
(
callback
)
{
{
'test'
:
'abc'
},
Item
.
bulkCreate
([
{
'test'
:
'def'
},
{
'test'
:
'abc'
},
{
'test'
:
'ghi'
},
{
'test'
:
'def'
},
{
'test'
:
'jkl'
}
{
'test'
:
'ghi'
},
]).
done
(
function
()
{
{
'test'
:
'jkl'
}
Item
.
findAll
({
order
:
[
'id'
]}).
done
(
callback
)
]).
done
(
function
()
{
})
Item
.
findAll
().
done
(
callback
)
},
})
orders
:
function
(
callback
)
{
},
Order
.
bulkCreate
([
orders
:
function
(
callback
)
{
{
'position'
:
2
},
Order
.
bulkCreate
([
{
'position'
:
3
},
{
'position'
:
2
},
{
'position'
:
1
}
{
'position'
:
3
},
]).
done
(
function
()
{
{
'position'
:
1
}
Order
.
findAll
({
order
:
[
'id'
]}).
done
(
callback
)
]).
done
(
function
()
{
})
Order
.
findAll
().
done
(
callback
)
},
})
associate
:
[
'users'
,
'items'
,
'orders'
,
function
(
callback
,
results
)
{
},
var
chainer
=
new
Sequelize
.
Utils
.
QueryChainer
()
associate
:
[
'users'
,
'items'
,
'orders'
,
function
(
callback
,
results
)
{
var
chainer
=
new
Sequelize
.
Utils
.
QueryChainer
()
var
user1
=
results
.
users
[
0
]
var
user1
=
results
.
users
[
0
]
var
user2
=
results
.
users
[
1
]
var
user2
=
results
.
users
[
1
]
var
user3
=
results
.
users
[
2
]
var
user3
=
results
.
users
[
2
]
var
item1
=
results
.
items
[
0
]
var
item1
=
results
.
items
[
0
]
var
item2
=
results
.
items
[
1
]
var
item2
=
results
.
items
[
1
]
var
item3
=
results
.
items
[
2
]
var
item3
=
results
.
items
[
2
]
var
item4
=
results
.
items
[
3
]
var
item4
=
results
.
items
[
3
]
var
order1
=
results
.
orders
[
0
]
var
order1
=
results
.
orders
[
0
]
var
order2
=
results
.
orders
[
1
]
var
order2
=
results
.
orders
[
1
]
var
order3
=
results
.
orders
[
2
]
var
order3
=
results
.
orders
[
2
]
chainer
.
add
(
user1
.
setItemA
(
item1
))
chainer
.
add
(
user1
.
setItemA
(
item1
))
chainer
.
add
(
user1
.
setItemB
(
item2
))
chainer
.
add
(
user1
.
setItemB
(
item2
))
chainer
.
add
(
user1
.
setOrder
(
order3
))
chainer
.
add
(
user1
.
setOrder
(
order3
))
chainer
.
add
(
user2
.
setItemA
(
item3
))
chainer
.
add
(
user2
.
setItemA
(
item3
))
chainer
.
add
(
user2
.
setItemB
(
item4
))
chainer
.
add
(
user2
.
setItemB
(
item4
))
chainer
.
add
(
user2
.
setOrder
(
order2
))
chainer
.
add
(
user2
.
setOrder
(
order2
))
chainer
.
add
(
user3
.
setItemA
(
item1
))
chainer
.
add
(
user3
.
setItemA
(
item1
))
chainer
.
add
(
user3
.
setItemB
(
item4
))
chainer
.
add
(
user3
.
setItemB
(
item4
))
chainer
.
add
(
user3
.
setOrder
(
order1
))
chainer
.
add
(
user3
.
setOrder
(
order1
))
chainer
.
run
().
done
(
callback
)
chainer
.
run
().
done
(
callback
)
}]},
function
()
{
}]
User
.
findAll
({
'where'
:
{
'itemA.test'
:
'abc'
},
'include'
:
[{
'model'
:
Item
,
'as'
:
'itemA'
},
{
'model'
:
Item
,
'as'
:
'itemB'
},
Order
],
'order'
:
'order.position'
}).
done
(
function
(
err
,
as
)
{
},
function
()
{
expect
(
err
).
not
.
to
.
be
.
ok
User
.
findAll
({
expect
(
as
.
length
).
to
.
eql
(
2
)
'where'
:
{
'itemA.test'
:
'abc'
},
'include'
:
[
{
'model'
:
Item
,
'as'
:
'itemA'
},
{
'model'
:
Item
,
'as'
:
'itemB'
},
Order
],
'order'
:
[
'Order.position'
]
}).
done
(
function
(
err
,
as
)
{
expect
(
err
).
not
.
to
.
be
.
ok
expect
(
as
.
length
).
to
.
eql
(
2
)
expect
(
as
[
0
].
itemA
[
0
]
.
test
).
to
.
eql
(
'abc'
)
expect
(
as
[
0
].
itemA
.
test
).
to
.
eql
(
'abc'
)
expect
(
as
[
1
].
itemA
[
0
]
.
test
).
to
.
eql
(
'abc'
)
expect
(
as
[
1
].
itemA
.
test
).
to
.
eql
(
'abc'
)
expect
(
as
[
0
].
order
.
position
).
to
.
eql
(
1
)
expect
(
as
[
0
].
order
.
position
).
to
.
eql
(
1
)
expect
(
as
[
1
].
order
.
position
).
to
.
eql
(
3
)
expect
(
as
[
1
].
order
.
position
).
to
.
eql
(
2
)
done
()
done
()
})
})
})
})
})
})
...
...
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