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 a0512041
authored
Feb 28, 2014
by
Mick Hansen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
non-integer/non-id primary key support for has one
1 parent
8b036963
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
79 additions
and
77 deletions
lib/associations/belongs-to.js
lib/associations/has-one.js
test/associations/belongs-to.test.js
test/associations/has-one.test.js
lib/associations/belongs-to.js
View file @
a051204
...
@@ -21,7 +21,6 @@ module.exports = (function() {
...
@@ -21,7 +21,6 @@ module.exports = (function() {
}
}
if
(
!
this
.
options
.
foreignKey
)
{
if
(
!
this
.
options
.
foreignKey
)
{
console
.
log
(
this
.
as
);
this
.
options
.
foreignKey
=
Utils
.
_
.
camelizeIf
(
this
.
options
.
foreignKey
=
Utils
.
_
.
camelizeIf
(
[
this
.
as
,
this
.
target
.
primaryKeyAttribute
].
join
(
"_"
),
[
this
.
as
,
this
.
target
.
primaryKeyAttribute
].
join
(
"_"
),
!
this
.
source
.
options
.
underscored
!
this
.
source
.
options
.
underscored
...
@@ -60,9 +59,15 @@ module.exports = (function() {
...
@@ -60,9 +59,15 @@ module.exports = (function() {
var
association
=
this
var
association
=
this
instancePrototype
[
this
.
accessors
.
get
]
=
function
(
params
)
{
instancePrototype
[
this
.
accessors
.
get
]
=
function
(
params
)
{
var
where
=
{}
params
=
params
||
{}
params
=
params
||
{}
params
.
where
=
params
.
where
||
{}
params
.
where
=
[
params
.
where
]
||
[]
params
.
where
[
association
.
targetIdentifier
]
=
this
.
get
(
association
.
identifier
)
where
[
association
.
targetIdentifier
]
=
this
.
get
(
association
.
identifier
)
params
.
where
.
push
(
where
)
params
.
where
=
new
Utils
.
and
(
params
.
where
)
return
association
.
target
.
find
(
params
)
return
association
.
target
.
find
(
params
)
}
}
...
@@ -74,8 +79,8 @@ module.exports = (function() {
...
@@ -74,8 +79,8 @@ module.exports = (function() {
BelongsTo
.
prototype
.
injectSetter
=
function
(
instancePrototype
)
{
BelongsTo
.
prototype
.
injectSetter
=
function
(
instancePrototype
)
{
var
association
=
this
var
association
=
this
instancePrototype
[
this
.
accessors
.
set
]
=
function
(
i
nstance
,
options
)
{
instancePrototype
[
this
.
accessors
.
set
]
=
function
(
associatedI
nstance
,
options
)
{
this
.
set
(
association
.
identifier
,
instance
?
i
nstance
[
association
.
targetIdentifier
]
:
null
)
this
.
set
(
association
.
identifier
,
associatedInstance
?
associatedI
nstance
[
association
.
targetIdentifier
]
:
null
)
options
=
Utils
.
_
.
extend
({
options
=
Utils
.
_
.
extend
({
fields
:
[
association
.
identifier
],
fields
:
[
association
.
identifier
],
...
...
lib/associations/has-one.js
View file @
a051204
...
@@ -10,23 +10,25 @@ module.exports = (function() {
...
@@ -10,23 +10,25 @@ module.exports = (function() {
this
.
target
=
targetDAO
this
.
target
=
targetDAO
this
.
options
=
options
this
.
options
=
options
this
.
isSingleAssociation
=
true
this
.
isSingleAssociation
=
true
this
.
isSelfAssociation
=
(
this
.
source
.
name
==
this
.
target
.
name
)
this
.
isSelfAssociation
=
(
this
.
source
==
this
.
target
)
this
.
as
=
this
.
options
.
as
this
.
as
=
this
.
options
.
as
if
(
this
.
isSelfAssociation
&&
!
this
.
options
.
foreignKey
&&
!!
this
.
as
)
{
this
.
options
.
foreignKey
=
Utils
.
_
.
underscoredIf
(
Utils
.
singularize
(
this
.
as
,
this
.
target
.
options
.
language
)
+
"Id"
,
this
.
target
.
options
.
underscored
)
}
if
(
this
.
as
)
{
if
(
this
.
as
)
{
this
.
isAliased
=
true
this
.
isAliased
=
true
}
else
{
}
else
{
this
.
as
=
Utils
.
singularize
(
this
.
target
.
name
,
this
.
target
.
options
.
language
)
this
.
as
=
Utils
.
singularize
(
this
.
target
.
name
,
this
.
target
.
options
.
language
)
}
}
this
.
associationAccessor
=
this
.
isSelfAssociation
if
(
!
this
.
options
.
foreignKey
)
{
?
Utils
.
combineTableNames
(
this
.
target
.
name
,
this
.
as
)
this
.
options
.
foreignKey
=
Utils
.
_
.
camelizeIf
(
:
this
.
as
[
this
.
source
.
name
,
this
.
source
.
primaryKeyAttribute
].
join
(
"_"
),
!
this
.
source
.
options
.
underscored
)
}
this
.
identifier
=
this
.
options
.
foreignKey
this
.
sourceIdentifier
=
this
.
source
.
primaryKeyAttribute
this
.
associationAccessor
=
this
.
as
this
.
options
.
useHooks
=
options
.
useHooks
this
.
options
.
useHooks
=
options
.
useHooks
this
.
accessors
=
{
this
.
accessors
=
{
...
@@ -39,10 +41,8 @@ module.exports = (function() {
...
@@ -39,10 +41,8 @@ module.exports = (function() {
// the id is in the target table
// the id is in the target table
HasOne
.
prototype
.
injectAttributes
=
function
()
{
HasOne
.
prototype
.
injectAttributes
=
function
()
{
var
newAttributes
=
{}
var
newAttributes
=
{}
,
sourceKeys
=
Object
.
keys
(
this
.
source
.
primaryKeys
)
,
keyType
=
this
.
source
.
rawAttributes
[
this
.
sourceIdentifier
].
type
,
keyType
=
((
this
.
source
.
hasPrimaryKeys
&&
sourceKeys
.
length
===
1
)
?
this
.
source
.
rawAttributes
[
sourceKeys
[
0
]].
type
:
DataTypes
.
INTEGER
)
this
.
identifier
=
this
.
options
.
foreignKey
||
Utils
.
_
.
underscoredIf
(
Utils
.
singularize
(
this
.
source
.
name
,
this
.
source
.
options
.
language
)
+
"Id"
,
this
.
source
.
options
.
underscored
)
newAttributes
[
this
.
identifier
]
=
{
type
:
this
.
options
.
keyType
||
keyType
}
newAttributes
[
this
.
identifier
]
=
{
type
:
this
.
options
.
keyType
||
keyType
}
Utils
.
_
.
defaults
(
this
.
target
.
rawAttributes
,
newAttributes
)
Utils
.
_
.
defaults
(
this
.
target
.
rawAttributes
,
newAttributes
)
Helpers
.
addForeignKeyConstraints
(
this
.
target
.
rawAttributes
[
this
.
identifier
],
this
.
source
,
this
.
target
,
this
.
options
)
Helpers
.
addForeignKeyConstraints
(
this
.
target
.
rawAttributes
[
this
.
identifier
],
this
.
source
,
this
.
target
,
this
.
options
)
...
@@ -52,81 +52,54 @@ module.exports = (function() {
...
@@ -52,81 +52,54 @@ module.exports = (function() {
return
this
return
this
}
}
HasOne
.
prototype
.
injectGetter
=
function
(
obj
)
{
HasOne
.
prototype
.
injectGetter
=
function
(
instancePrototype
)
{
var
self
=
this
var
association
=
this
,
smart
obj
[
this
.
accessors
.
get
]
=
function
(
params
)
{
instancePrototype
[
this
.
accessors
.
get
]
=
function
(
params
)
{
var
primaryKeys
=
Object
.
keys
(
this
.
Model
.
primaryKeys
)
var
where
=
{}
,
primaryKey
=
primaryKeys
.
length
===
1
?
primaryKeys
[
0
]
:
'id'
,
where
=
{}
,
id
=
this
[
primaryKey
]
||
this
.
id
where
[
self
.
identifier
]
=
id
params
=
params
||
{}
params
.
where
=
[
params
.
where
]
||
[]
if
(
!
Utils
.
_
.
isUndefined
(
params
))
{
where
[
association
.
identifier
]
=
this
.
get
(
association
.
sourceIdentifier
)
if
(
!
Utils
.
_
.
isUndefined
(
params
.
attributes
))
{
params
.
where
.
push
(
where
)
params
=
Utils
.
_
.
extend
({
where
:
where
},
params
)
}
}
else
{
params
=
{
where
:
where
}
}
smart
=
Utils
.
smartWhere
(
params
.
where
||
[],
self
.
target
.
daoFactoryManager
.
sequelize
.
options
.
dialect
)
params
.
where
=
new
Utils
.
and
(
params
.
where
)
smart
=
Utils
.
compileSmartWhere
.
call
(
self
.
target
,
smart
,
self
.
target
.
daoFactoryManager
.
sequelize
.
options
.
dialect
)
if
(
smart
.
length
>
0
)
{
params
.
where
=
smart
}
var
options
=
{}
return
association
.
target
.
find
(
params
)
if
(
params
.
transaction
)
{
options
.
transaction
=
params
.
transaction
;
delete
params
.
transaction
;
}
return
self
.
target
.
find
(
params
,
options
)
}
}
return
this
return
this
}
}
HasOne
.
prototype
.
injectSetter
=
function
(
obj
)
{
HasOne
.
prototype
.
injectSetter
=
function
(
instancePrototype
)
{
var
self
=
this
var
association
=
this
obj
[
this
.
accessors
.
set
]
=
function
(
associatedObject
,
options
)
{
instancePrototype
[
this
.
accessors
.
set
]
=
function
(
associatedInstance
,
options
)
{
var
instance
=
this
var
instance
=
this
,
instanceKeys
=
Object
.
keys
(
instance
.
Model
.
primaryKeys
)
,
instanceKey
=
instanceKeys
.
length
===
1
?
instanceKeys
[
0
]
:
'id'
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
instance
[
self
.
accessors
.
get
]().
success
(
function
(
oldObj
)
{
instance
[
association
.
accessors
.
get
]().
success
(
function
(
oldInstance
)
{
if
(
oldObj
)
{
if
(
oldInstance
)
{
oldObj
[
self
.
identifier
]
=
null
oldInstance
[
association
.
identifier
]
=
null
oldObj
oldInstance
.
save
(
.
save
(
Utils
.
_
.
extend
({},
options
,
{
Utils
.
_
.
extend
({},
options
,
{
fields
:
[
association
.
identifier
],
fields
:
[
self
.
identifier
],
allowNull
:
[
association
.
identifier
],
allowNull
:
[
self
.
identifier
],
association
:
true
association
:
true
})
}))
)
.
success
(
function
()
{
.
success
(
function
()
{
if
(
associatedObject
)
{
if
(
associatedInstance
)
{
associatedObject
[
self
.
identifier
]
=
instance
[
instanceKey
]
associatedInstance
.
set
(
association
.
identifier
,
instance
.
get
(
association
.
sourceIdentifier
))
associatedObject
associatedInstance
.
save
(
options
).
proxy
(
emitter
)
.
save
(
options
)
.
success
(
function
()
{
emitter
.
emit
(
'success'
,
associatedObject
)
})
.
error
(
function
(
err
)
{
emitter
.
emit
(
'error'
,
err
)
})
}
else
{
}
else
{
emitter
.
emit
(
'success'
,
null
)
emitter
.
emit
(
'success'
,
null
)
}
}
})
})
}
else
{
}
else
{
if
(
associatedObject
)
{
if
(
associatedInstance
)
{
associatedObject
[
self
.
identifier
]
=
instance
[
instanceKey
]
associatedInstance
.
set
(
association
.
identifier
,
instance
.
get
(
association
.
sourceIdentifier
))
associatedObject
associatedInstance
.
save
(
options
).
proxy
(
emitter
)
.
save
(
options
)
.
success
(
function
()
{
emitter
.
emit
(
'success'
,
associatedObject
)
})
.
error
(
function
(
err
)
{
emitter
.
emit
(
'error'
,
err
)
})
}
else
{
}
else
{
emitter
.
emit
(
'success'
,
null
)
emitter
.
emit
(
'success'
,
null
)
}
}
...
@@ -138,10 +111,10 @@ module.exports = (function() {
...
@@ -138,10 +111,10 @@ module.exports = (function() {
return
this
return
this
}
}
HasOne
.
prototype
.
injectCreator
=
function
(
obj
)
{
HasOne
.
prototype
.
injectCreator
=
function
(
instancePrototype
)
{
var
self
=
this
var
association
=
this
obj
[
this
.
accessors
.
create
]
=
function
(
values
,
fieldsOrOptions
)
{
instancePrototype
[
this
.
accessors
.
create
]
=
function
(
values
,
fieldsOrOptions
)
{
var
instance
=
this
var
instance
=
this
,
options
=
{}
,
options
=
{}
...
@@ -150,11 +123,11 @@ module.exports = (function() {
...
@@ -150,11 +123,11 @@ module.exports = (function() {
}
}
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
self
.
target
association
.
target
.
create
(
values
,
fieldsOrOptions
)
.
create
(
values
,
fieldsOrOptions
)
.
proxy
(
emitter
,
{
events
:
[
'error'
,
'sql'
]
})
.
proxy
(
emitter
,
{
events
:
[
'error'
,
'sql'
]
})
.
success
(
function
(
newAssociatedObject
)
{
.
success
(
function
(
associationInstance
)
{
instance
[
self
.
accessors
.
set
](
newAssociatedObject
,
options
)
instance
[
association
.
accessors
.
set
](
associationInstance
,
options
)
.
proxy
(
emitter
)
.
proxy
(
emitter
)
})
})
}).
run
()
}).
run
()
...
...
test/associations/belongs-to.test.js
View file @
a051204
...
@@ -3,6 +3,7 @@ var chai = require('chai')
...
@@ -3,6 +3,7 @@ var chai = require('chai')
,
expect
=
chai
.
expect
,
expect
=
chai
.
expect
,
Support
=
require
(
__dirname
+
'/../support'
)
,
Support
=
require
(
__dirname
+
'/../support'
)
,
DataTypes
=
require
(
__dirname
+
"/../../lib/data-types"
)
,
DataTypes
=
require
(
__dirname
+
"/../../lib/data-types"
)
,
Sequelize
=
require
(
'../../index'
)
chai
.
Assertion
.
includeStack
=
true
chai
.
Assertion
.
includeStack
=
true
...
@@ -51,6 +52,29 @@ describe(Support.getTestDialectTeaser("BelongsTo"), function() {
...
@@ -51,6 +52,29 @@ describe(Support.getTestDialectTeaser("BelongsTo"), function() {
})
})
})
})
})
})
it
(
'should be able to handle a where object that\'s a first class citizen.'
,
function
(
done
)
{
var
User
=
this
.
sequelize
.
define
(
'UserXYZ'
,
{
username
:
Sequelize
.
STRING
,
gender
:
Sequelize
.
STRING
})
,
Task
=
this
.
sequelize
.
define
(
'TaskXYZ'
,
{
title
:
Sequelize
.
STRING
,
status
:
Sequelize
.
STRING
})
Task
.
belongsTo
(
User
)
User
.
sync
({
force
:
true
}).
success
(
function
()
{
Task
.
sync
({
force
:
true
}).
success
(
function
()
{
User
.
create
({
username
:
'foo'
,
gender
:
'male'
}).
success
(
function
(
user
)
{
User
.
create
({
username
:
'bar'
,
gender
:
'female'
}).
success
(
function
(
falsePositiveCheck
)
{
Task
.
create
({
title
:
'task'
,
status
:
'inactive'
}).
success
(
function
(
task
)
{
task
.
setUserXYZ
(
user
).
success
(
function
()
{
task
.
getUserXYZ
({
where
:
[
'gender = ?'
,
'female'
]}).
success
(
function
(
user
)
{
expect
(
user
).
to
.
be
.
null
done
()
})
})
})
})
})
})
})
})
})
})
describe
(
'setAssociation'
,
function
()
{
describe
(
'setAssociation'
,
function
()
{
...
...
test/associations/has-one.test.js
View file @
a051204
...
@@ -370,7 +370,7 @@ describe(Support.getTestDialectTeaser("HasOne"), function() {
...
@@ -370,7 +370,7 @@ describe(Support.getTestDialectTeaser("HasOne"), function() {
Group
.
hasOne
(
User
)
Group
.
hasOne
(
User
)
self
.
sequelize
.
sync
({
force
:
true
}).
success
(
function
()
{
self
.
sequelize
.
sync
({
force
:
true
}).
success
(
function
()
{
expect
(
User
.
rawAttributes
.
GroupPKBT
Id
.
type
.
toString
()).
to
.
equal
(
Sequelize
.
STRING
.
toString
())
expect
(
User
.
rawAttributes
.
GroupPKBT
Name
.
type
.
toString
()).
to
.
equal
(
Sequelize
.
STRING
.
toString
())
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