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 559e1cef
authored
Apr 30, 2013
by
Martin Aspeli
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Basic implementation of capturing foreign key status
1 parent
8f861533
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
41 additions
and
0 deletions
lib/associations/belongs-to.js
lib/associations/has-many.js
lib/associations/has-one.js
spec/associations/has-many.spec.js
lib/associations/belongs-to.js
View file @
559e1ce
...
@@ -24,6 +24,12 @@ module.exports = (function() {
...
@@ -24,6 +24,12 @@ module.exports = (function() {
this
.
identifier
=
this
.
options
.
foreignKey
||
Utils
.
_
.
underscoredIf
(
Utils
.
singularize
(
this
.
target
.
tableName
)
+
"Id"
,
this
.
source
.
options
.
underscored
)
this
.
identifier
=
this
.
options
.
foreignKey
||
Utils
.
_
.
underscoredIf
(
Utils
.
singularize
(
this
.
target
.
tableName
)
+
"Id"
,
this
.
source
.
options
.
underscored
)
newAttributes
[
this
.
identifier
]
=
{
type
:
DataTypes
.
INTEGER
}
newAttributes
[
this
.
identifier
]
=
{
type
:
DataTypes
.
INTEGER
}
if
(
typeof
this
.
options
.
foreignKey
===
"undefined"
||
this
.
options
.
foreignKey
===
true
)
{
newAttributes
[
this
.
identifier
].
references
=
this
.
target
.
tableName
,
newAttributes
[
this
.
identifier
].
referencesKeys
=
Utils
.
_
.
reduce
(
this
.
source
.
rawAttributes
,
function
(
memo
,
value
,
key
)
{
if
(
value
.
primaryKey
)
{
memo
.
push
(
key
)
}
return
memo
},
[])
newAttributes
[
this
.
identifier
].
onDelete
=
this
.
options
.
onDelete
,
newAttributes
[
this
.
identifier
].
onUpdate
=
this
.
options
.
onUpdate
}
Utils
.
_
.
defaults
(
this
.
source
.
rawAttributes
,
newAttributes
)
Utils
.
_
.
defaults
(
this
.
source
.
rawAttributes
,
newAttributes
)
// Sync attributes to DAO proto each time a new assoc is added
// Sync attributes to DAO proto each time a new assoc is added
...
...
lib/associations/has-many.js
View file @
559e1ce
...
@@ -65,6 +65,12 @@ module.exports = (function() {
...
@@ -65,6 +65,12 @@ module.exports = (function() {
}
else
{
}
else
{
var
newAttributes
=
{}
var
newAttributes
=
{}
newAttributes
[
this
.
identifier
]
=
{
type
:
DataTypes
.
INTEGER
}
newAttributes
[
this
.
identifier
]
=
{
type
:
DataTypes
.
INTEGER
}
if
(
typeof
this
.
options
.
foreignKey
===
"undefined"
||
this
.
options
.
foreignKey
===
true
)
{
newAttributes
[
this
.
identifier
].
references
=
this
.
source
.
tableName
newAttributes
[
this
.
identifier
].
referencesKeys
=
Utils
.
_
.
reduce
(
this
.
source
.
rawAttributes
,
function
(
memo
,
value
,
key
)
{
if
(
value
.
primaryKey
)
{
memo
.
push
(
key
)
}
return
memo
},
[])
newAttributes
[
this
.
identifier
].
onDelete
=
this
.
options
.
onDelete
newAttributes
[
this
.
identifier
].
onUpdate
=
this
.
options
.
onUpdate
}
Utils
.
_
.
defaults
(
this
.
target
.
rawAttributes
,
newAttributes
)
Utils
.
_
.
defaults
(
this
.
target
.
rawAttributes
,
newAttributes
)
}
}
...
...
lib/associations/has-one.js
View file @
559e1ce
...
@@ -29,6 +29,12 @@ module.exports = (function() {
...
@@ -29,6 +29,12 @@ module.exports = (function() {
this
.
identifier
=
this
.
options
.
foreignKey
||
Utils
.
_
.
underscoredIf
(
Utils
.
singularize
(
this
.
source
.
tableName
)
+
"Id"
,
this
.
options
.
underscored
)
this
.
identifier
=
this
.
options
.
foreignKey
||
Utils
.
_
.
underscoredIf
(
Utils
.
singularize
(
this
.
source
.
tableName
)
+
"Id"
,
this
.
options
.
underscored
)
newAttributes
[
this
.
identifier
]
=
{
type
:
DataTypes
.
INTEGER
}
newAttributes
[
this
.
identifier
]
=
{
type
:
DataTypes
.
INTEGER
}
if
(
typeof
this
.
options
.
foreignKey
===
"undefined"
||
this
.
options
.
foreignKey
===
true
)
{
newAttributes
[
this
.
identifier
].
references
=
this
.
source
.
tableName
,
newAttributes
[
this
.
identifier
].
referencesKeys
=
Utils
.
_
.
reduce
(
this
.
source
.
rawAttributes
,
function
(
memo
,
value
,
key
)
{
if
(
value
.
primaryKey
)
{
memo
.
push
(
key
)
}
return
memo
},
[])
newAttributes
[
this
.
identifier
].
onDelete
=
this
.
options
.
onDelete
,
newAttributes
[
this
.
identifier
].
onUpdate
=
this
.
options
.
onUpdate
}
Utils
.
_
.
defaults
(
this
.
target
.
rawAttributes
,
newAttributes
)
Utils
.
_
.
defaults
(
this
.
target
.
rawAttributes
,
newAttributes
)
// Sync attributes to DAO proto each time a new assoc is added
// Sync attributes to DAO proto each time a new assoc is added
...
...
spec/associations/has-many.spec.js
View file @
559e1ce
...
@@ -322,5 +322,28 @@ describe(Helpers.getTestDialectTeaser("HasMany"), function() {
...
@@ -322,5 +322,28 @@ describe(Helpers.getTestDialectTeaser("HasMany"), function() {
})
})
})
})
})
})
describe
(
"Foreign key constraints"
,
function
()
{
it
(
"can cascade deletes"
,
function
(
done
)
{
var
User
=
this
.
sequelize
.
define
(
'User'
,
{
username
:
Sequelize
.
STRING
})
,
Task
=
this
.
sequelize
.
define
(
'Task'
,
{
title
:
Sequelize
.
STRING
})
User
.
hasMany
(
Task
,
{
onDelete
:
'cascade'
})
this
.
sequelize
.
sync
({
force
:
true
}).
success
(
function
()
{
User
.
create
({
username
:
'foo'
}).
success
(
function
(
user
)
{
Task
.
create
({
title
:
'task'
}).
success
(
function
(
task
)
{
user
.
setTasks
([
task
]).
success
(
function
()
{
debugger
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