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
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
81 additions
and
79 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() {
}
if
(
!
this
.
options
.
foreignKey
)
{
console
.
log
(
this
.
as
);
this
.
options
.
foreignKey
=
Utils
.
_
.
camelizeIf
(
[
this
.
as
,
this
.
target
.
primaryKeyAttribute
].
join
(
"_"
),
!
this
.
source
.
options
.
underscored
...
...
@@ -60,9 +59,15 @@ module.exports = (function() {
var
association
=
this
instancePrototype
[
this
.
accessors
.
get
]
=
function
(
params
)
{
var
where
=
{}
params
=
params
||
{}
params
.
where
=
params
.
where
||
{}
params
.
where
[
association
.
targetIdentifier
]
=
this
.
get
(
association
.
identifier
)
params
.
where
=
[
params
.
where
]
||
[]
where
[
association
.
targetIdentifier
]
=
this
.
get
(
association
.
identifier
)
params
.
where
.
push
(
where
)
params
.
where
=
new
Utils
.
and
(
params
.
where
)
return
association
.
target
.
find
(
params
)
}
...
...
@@ -74,8 +79,8 @@ module.exports = (function() {
BelongsTo
.
prototype
.
injectSetter
=
function
(
instancePrototype
)
{
var
association
=
this
instancePrototype
[
this
.
accessors
.
set
]
=
function
(
i
nstance
,
options
)
{
this
.
set
(
association
.
identifier
,
instance
?
i
nstance
[
association
.
targetIdentifier
]
:
null
)
instancePrototype
[
this
.
accessors
.
set
]
=
function
(
associatedI
nstance
,
options
)
{
this
.
set
(
association
.
identifier
,
associatedInstance
?
associatedI
nstance
[
association
.
targetIdentifier
]
:
null
)
options
=
Utils
.
_
.
extend
({
fields
:
[
association
.
identifier
],
...
...
lib/associations/has-one.js
View file @
a051204
...
...
@@ -10,12 +10,8 @@ module.exports = (function() {
this
.
target
=
targetDAO
this
.
options
=
options
this
.
isSingleAssociation
=
true
this
.
isSelfAssociation
=
(
this
.
source
.
name
==
this
.
target
.
name
)
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
)
}
this
.
isSelfAssociation
=
(
this
.
source
==
this
.
target
)
this
.
as
=
this
.
options
.
as
if
(
this
.
as
)
{
this
.
isAliased
=
true
...
...
@@ -23,10 +19,16 @@ module.exports = (function() {
this
.
as
=
Utils
.
singularize
(
this
.
target
.
name
,
this
.
target
.
options
.
language
)
}
this
.
associationAccessor
=
this
.
isSelfAssociation
?
Utils
.
combineTableNames
(
this
.
target
.
name
,
this
.
as
)
:
this
.
as
if
(
!
this
.
options
.
foreignKey
)
{
this
.
options
.
foreignKey
=
Utils
.
_
.
camelizeIf
(
[
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
.
accessors
=
{
...
...
@@ -39,10 +41,8 @@ module.exports = (function() {
// the id is in the target table
HasOne
.
prototype
.
injectAttributes
=
function
()
{
var
newAttributes
=
{}
,
sourceKeys
=
Object
.
keys
(
this
.
source
.
primaryKeys
)
,
keyType
=
((
this
.
source
.
hasPrimaryKeys
&&
sourceKeys
.
length
===
1
)
?
this
.
source
.
rawAttributes
[
sourceKeys
[
0
]].
type
:
DataTypes
.
INTEGER
)
,
keyType
=
this
.
source
.
rawAttributes
[
this
.
sourceIdentifier
].
type
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
}
Utils
.
_
.
defaults
(
this
.
target
.
rawAttributes
,
newAttributes
)
Helpers
.
addForeignKeyConstraints
(
this
.
target
.
rawAttributes
[
this
.
identifier
],
this
.
source
,
this
.
target
,
this
.
options
)
...
...
@@ -52,81 +52,54 @@ module.exports = (function() {
return
this
}
HasOne
.
prototype
.
injectGetter
=
function
(
obj
)
{
var
self
=
this
,
smart
HasOne
.
prototype
.
injectGetter
=
function
(
instancePrototype
)
{
var
association
=
this
obj
[
this
.
accessors
.
get
]
=
function
(
params
)
{
var
primaryKeys
=
Object
.
keys
(
this
.
Model
.
primaryKeys
)
,
primaryKey
=
primaryKeys
.
length
===
1
?
primaryKeys
[
0
]
:
'id'
,
where
=
{}
,
id
=
this
[
primaryKey
]
||
this
.
id
instancePrototype
[
this
.
accessors
.
get
]
=
function
(
params
)
{
var
where
=
{}
where
[
self
.
identifier
]
=
id
params
=
params
||
{}
params
.
where
=
[
params
.
where
]
||
[]
if
(
!
Utils
.
_
.
isUndefined
(
params
))
{
if
(
!
Utils
.
_
.
isUndefined
(
params
.
attributes
))
{
params
=
Utils
.
_
.
extend
({
where
:
where
},
params
)
}
}
else
{
params
=
{
where
:
where
}
}
where
[
association
.
identifier
]
=
this
.
get
(
association
.
sourceIdentifier
)
params
.
where
.
push
(
where
)
smart
=
Utils
.
smartWhere
(
params
.
where
||
[],
self
.
target
.
daoFactoryManager
.
sequelize
.
options
.
dialect
)
smart
=
Utils
.
compileSmartWhere
.
call
(
self
.
target
,
smart
,
self
.
target
.
daoFactoryManager
.
sequelize
.
options
.
dialect
)
if
(
smart
.
length
>
0
)
{
params
.
where
=
smart
}
params
.
where
=
new
Utils
.
and
(
params
.
where
)
var
options
=
{}
if
(
params
.
transaction
)
{
options
.
transaction
=
params
.
transaction
;
delete
params
.
transaction
;
}
return
self
.
target
.
find
(
params
,
options
)
return
association
.
target
.
find
(
params
)
}
return
this
}
HasOne
.
prototype
.
injectSetter
=
function
(
obj
)
{
var
self
=
this
HasOne
.
prototype
.
injectSetter
=
function
(
instancePrototype
)
{
var
association
=
this
obj
[
this
.
accessors
.
set
]
=
function
(
associatedObject
,
options
)
{
instancePrototype
[
this
.
accessors
.
set
]
=
function
(
associatedInstance
,
options
)
{
var
instance
=
this
,
instanceKeys
=
Object
.
keys
(
instance
.
Model
.
primaryKeys
)
,
instanceKey
=
instanceKeys
.
length
===
1
?
instanceKeys
[
0
]
:
'id'
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
instance
[
self
.
accessors
.
get
]().
success
(
function
(
oldObj
)
{
if
(
oldObj
)
{
oldObj
[
self
.
identifier
]
=
null
oldObj
.
save
(
Utils
.
_
.
extend
({},
options
,
{
fields
:
[
self
.
identifier
],
allowNull
:
[
self
.
identifier
],
association
:
true
})
)
instance
[
association
.
accessors
.
get
]().
success
(
function
(
oldInstance
)
{
if
(
oldInstance
)
{
oldInstance
[
association
.
identifier
]
=
null
oldInstance
.
save
(
Utils
.
_
.
extend
({},
options
,
{
fields
:
[
association
.
identifier
],
allowNull
:
[
association
.
identifier
],
association
:
true
}))
.
success
(
function
()
{
if
(
associatedObject
)
{
associatedObject
[
self
.
identifier
]
=
instance
[
instanceKey
]
associatedObject
.
save
(
options
)
.
success
(
function
()
{
emitter
.
emit
(
'success'
,
associatedObject
)
})
.
error
(
function
(
err
)
{
emitter
.
emit
(
'error'
,
err
)
})
if
(
associatedInstance
)
{
associatedInstance
.
set
(
association
.
identifier
,
instance
.
get
(
association
.
sourceIdentifier
))
associatedInstance
.
save
(
options
).
proxy
(
emitter
)
}
else
{
emitter
.
emit
(
'success'
,
null
)
}
})
}
else
{
if
(
associatedObject
)
{
associatedObject
[
self
.
identifier
]
=
instance
[
instanceKey
]
associatedObject
.
save
(
options
)
.
success
(
function
()
{
emitter
.
emit
(
'success'
,
associatedObject
)
})
.
error
(
function
(
err
)
{
emitter
.
emit
(
'error'
,
err
)
})
if
(
associatedInstance
)
{
associatedInstance
.
set
(
association
.
identifier
,
instance
.
get
(
association
.
sourceIdentifier
))
associatedInstance
.
save
(
options
).
proxy
(
emitter
)
}
else
{
emitter
.
emit
(
'success'
,
null
)
}
...
...
@@ -138,10 +111,10 @@ module.exports = (function() {
return
this
}
HasOne
.
prototype
.
injectCreator
=
function
(
obj
)
{
var
self
=
this
HasOne
.
prototype
.
injectCreator
=
function
(
instancePrototype
)
{
var
association
=
this
obj
[
this
.
accessors
.
create
]
=
function
(
values
,
fieldsOrOptions
)
{
instancePrototype
[
this
.
accessors
.
create
]
=
function
(
values
,
fieldsOrOptions
)
{
var
instance
=
this
,
options
=
{}
...
...
@@ -150,11 +123,11 @@ module.exports = (function() {
}
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
self
.
target
association
.
target
.
create
(
values
,
fieldsOrOptions
)
.
proxy
(
emitter
,
{
events
:
[
'error'
,
'sql'
]
})
.
success
(
function
(
newAssociatedObject
)
{
instance
[
self
.
accessors
.
set
](
newAssociatedObject
,
options
)
.
success
(
function
(
associationInstance
)
{
instance
[
association
.
accessors
.
set
](
associationInstance
,
options
)
.
proxy
(
emitter
)
})
}).
run
()
...
...
test/associations/belongs-to.test.js
View file @
a051204
...
...
@@ -3,6 +3,7 @@ var chai = require('chai')
,
expect
=
chai
.
expect
,
Support
=
require
(
__dirname
+
'/../support'
)
,
DataTypes
=
require
(
__dirname
+
"/../../lib/data-types"
)
,
Sequelize
=
require
(
'../../index'
)
chai
.
Assertion
.
includeStack
=
true
...
...
@@ -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
()
{
...
...
test/associations/has-one.test.js
View file @
a051204
...
...
@@ -370,7 +370,7 @@ describe(Support.getTestDialectTeaser("HasOne"), function() {
Group
.
hasOne
(
User
)
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
()
})
})
...
...
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