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 da9fdd6b
authored
Feb 28, 2014
by
Mick Hansen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
non-id/non-integer support for has many
1 parent
7fb45bf5
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
13 additions
and
10 deletions
lib/associations/belongs-to.js
lib/associations/has-many.js
test/associations/has-many.test.js
lib/associations/belongs-to.js
View file @
da9fdd6
...
...
@@ -42,9 +42,8 @@ module.exports = (function() {
// the id is in the source table
BelongsTo
.
prototype
.
injectAttributes
=
function
()
{
var
newAttributes
=
{}
,
keyType
=
this
.
target
.
rawAttributes
[
this
.
targetIdentifier
].
type
newAttributes
[
this
.
identifier
]
=
{
type
:
this
.
options
.
keyType
||
keyT
ype
}
newAttributes
[
this
.
identifier
]
=
{
type
:
this
.
options
.
keyType
||
this
.
target
.
rawAttributes
[
this
.
targetIdentifier
].
t
ype
}
Helpers
.
addForeignKeyConstraints
(
newAttributes
[
this
.
identifier
],
this
.
target
,
this
.
source
,
this
.
options
)
Utils
.
_
.
defaults
(
this
.
source
.
rawAttributes
,
newAttributes
)
...
...
lib/associations/has-many.js
View file @
da9fdd6
...
...
@@ -137,14 +137,20 @@ module.exports = (function() {
,
self
=
this
,
primaryKeyDeleted
=
false
this
.
identifier
=
this
.
options
.
foreignKey
||
Utils
.
_
.
camelizeIf
(
Utils
.
singularize
(
this
.
source
.
name
,
this
.
source
.
options
.
language
)
+
"_id"
,
!
this
.
source
.
options
.
underscored
)
this
.
identifier
=
this
.
options
.
foreignKey
||
Utils
.
_
.
camelizeIf
(
[
Utils
.
singularize
(
this
.
source
.
name
,
this
.
source
.
options
.
language
),
this
.
source
.
primaryKeyAttribute
].
join
(
"_"
),
!
this
.
source
.
options
.
underscored
)
// is there already a single sided association between the source and the target?
// or is the association on the model itself?
if
((
this
.
isSelfAssociation
&&
Object
(
this
.
through
)
===
this
.
through
)
||
doubleLinked
)
{
// remove the obsolete association identifier from the source
if
(
this
.
isSelfAssociation
)
{
this
.
foreignIdentifier
=
Utils
.
_
.
camelizeIf
((
this
.
options
.
as
||
this
.
target
.
name
)
+
'_id'
,
!
this
.
target
.
options
.
underscored
)
this
.
foreignIdentifier
=
Utils
.
_
.
camelizeIf
(
[(
this
.
options
.
as
||
this
.
target
.
name
),
this
.
target
.
primaryKeyAttribute
].
join
(
'_'
),
!
this
.
target
.
options
.
underscored
)
}
else
{
this
.
foreignIdentifier
=
this
.
targetAssociation
.
identifier
this
.
targetAssociation
.
foreignIdentifier
=
this
.
identifier
...
...
@@ -168,10 +174,8 @@ module.exports = (function() {
// define a new model, which connects the models
var
combinedTableAttributes
=
{}
var
sourceKeys
=
Object
.
keys
(
this
.
source
.
primaryKeys
);
var
sourceKeyType
=
((
!
this
.
source
.
hasPrimaryKeys
||
sourceKeys
.
length
!==
1
)
?
DataTypes
.
INTEGER
:
this
.
source
.
rawAttributes
[
sourceKeys
[
0
]].
type
)
var
targetKeys
=
Object
.
keys
(
this
.
target
.
primaryKeys
);
var
targetKeyType
=
((
!
this
.
target
.
hasPrimaryKeys
||
targetKeys
.
length
!==
1
)
?
DataTypes
.
INTEGER
:
this
.
target
.
rawAttributes
[
targetKeys
[
0
]].
type
)
var
sourceKeyType
=
this
.
source
.
rawAttributes
[
this
.
source
.
primaryKeyAttribute
].
type
var
targetKeyType
=
this
.
target
.
rawAttributes
[
this
.
target
.
primaryKeyAttribute
].
type
if
(
primaryKeyDeleted
)
{
combinedTableAttributes
[
this
.
identifier
]
=
{
type
:
sourceKeyType
,
primaryKey
:
true
}
...
...
@@ -190,7 +194,7 @@ module.exports = (function() {
}
}
else
{
var
newAttributes
=
{}
newAttributes
[
this
.
identifier
]
=
{
type
:
this
.
options
.
keyType
||
DataTypes
.
INTEGER
}
newAttributes
[
this
.
identifier
]
=
{
type
:
this
.
options
.
keyType
||
this
.
target
.
rawAttributes
[
this
.
target
.
primaryKeyAttribute
].
type
}
Helpers
.
addForeignKeyConstraints
(
newAttributes
[
this
.
identifier
],
this
.
source
,
this
.
target
,
this
.
options
)
Utils
.
_
.
defaults
(
this
.
target
.
rawAttributes
,
newAttributes
)
}
...
...
test/associations/has-many.test.js
View file @
da9fdd6
...
...
@@ -954,7 +954,7 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
_
.
each
([
self
.
UserTasks
,
self
.
UserTasks2
],
function
(
model
)
{
fk
=
Object
.
keys
(
model
.
options
.
uniqueKeys
)[
0
]
expect
(
model
.
options
.
uniqueKeys
[
fk
].
fields
).
to
.
deep
.
equal
([
'
taskId'
,
'u
serId'
])
expect
(
model
.
options
.
uniqueKeys
[
fk
].
fields
).
to
.
deep
.
equal
([
'
TaskId'
,
'U
serId'
])
})
})
})
...
...
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