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 620d8e77
authored
May 08, 2014
by
Mick Hansen
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'pola88-primary_key_on_has_many'
2 parents
2d5cf42c
54655ab2
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
48 additions
and
7 deletions
lib/dialects/postgres/query-generator.js
lib/model.js
lib/query-interface.js
test/associations/has-many.test.js
lib/dialects/postgres/query-generator.js
View file @
620d8e7
...
@@ -290,7 +290,7 @@ module.exports = (function() {
...
@@ -290,7 +290,7 @@ module.exports = (function() {
return
Utils
.
_
.
template
(
query
)(
replacements
)
return
Utils
.
_
.
template
(
query
)(
replacements
)
},
},
deleteQuery
:
function
(
tableName
,
where
,
options
,
factory
)
{
deleteQuery
:
function
(
tableName
,
where
,
options
,
model
)
{
options
=
options
||
{}
options
=
options
||
{}
tableName
=
Utils
.
removeTicks
(
this
.
quoteTable
(
tableName
),
'"'
)
tableName
=
Utils
.
removeTicks
(
this
.
quoteTable
(
tableName
),
'"'
)
...
@@ -305,8 +305,8 @@ module.exports = (function() {
...
@@ -305,8 +305,8 @@ module.exports = (function() {
primaryKeys
[
tableName
]
=
primaryKeys
[
tableName
]
||
[];
primaryKeys
[
tableName
]
=
primaryKeys
[
tableName
]
||
[];
if
(
!!
factory
&&
primaryKeys
[
tableName
].
length
<
1
)
{
if
(
!!
model
&&
primaryKeys
[
tableName
].
length
<
1
)
{
primaryKeys
[
tableName
]
=
Object
.
keys
(
factory
.
primaryKeys
)
primaryKeys
[
tableName
]
=
Object
.
keys
(
model
.
primaryKeys
)
}
}
var
query
=
"DELETE FROM <%= table %> WHERE <%= primaryKeys %> IN (SELECT <%= primaryKeysSelection %> FROM <%= table %> WHERE <%= where %><%= limit %>)"
var
query
=
"DELETE FROM <%= table %> WHERE <%= primaryKeys %> IN (SELECT <%= primaryKeysSelection %> FROM <%= table %> WHERE <%= where %><%= limit %>)"
...
...
lib/model.js
View file @
620d8e7
...
@@ -1278,10 +1278,10 @@ module.exports = (function() {
...
@@ -1278,10 +1278,10 @@ module.exports = (function() {
var
attrValueHash
=
{}
var
attrValueHash
=
{}
attrValueHash
[
self
.
_timestampAttributes
.
deletedAt
]
=
Utils
.
now
()
attrValueHash
[
self
.
_timestampAttributes
.
deletedAt
]
=
Utils
.
now
()
query
=
'bulkUpdate'
query
=
'bulkUpdate'
args
=
[
self
.
getTableName
(),
attrValueHash
,
where
]
args
=
[
self
.
getTableName
(),
attrValueHash
,
where
,
self
]
}
else
{
}
else
{
query
=
'bulkDelete'
query
=
'bulkDelete'
args
=
[
self
.
getTableName
(),
where
,
options
]
args
=
[
self
.
getTableName
(),
where
,
options
,
self
]
}
}
var
runQuery
=
function
(
err
,
records
)
{
var
runQuery
=
function
(
err
,
records
)
{
...
...
lib/query-interface.js
View file @
620d8e7
...
@@ -565,8 +565,8 @@ module.exports = (function() {
...
@@ -565,8 +565,8 @@ module.exports = (function() {
});
});
}
}
QueryInterface
.
prototype
.
bulkDelete
=
function
(
tableName
,
identifier
,
options
)
{
QueryInterface
.
prototype
.
bulkDelete
=
function
(
tableName
,
identifier
,
options
,
model
)
{
var
sql
=
this
.
QueryGenerator
.
deleteQuery
(
tableName
,
identifier
,
Utils
.
_
.
defaults
(
options
||
{},
{
limit
:
null
}))
var
sql
=
this
.
QueryGenerator
.
deleteQuery
(
tableName
,
identifier
,
Utils
.
_
.
defaults
(
options
||
{},
{
limit
:
null
})
,
model
)
return
this
.
queryAndEmit
([
sql
,
null
,
options
],
'bulkDelete'
,
options
)
return
this
.
queryAndEmit
([
sql
,
null
,
options
],
'bulkDelete'
,
options
)
}
}
...
...
test/associations/has-many.test.js
View file @
620d8e7
...
@@ -1120,6 +1120,47 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
...
@@ -1120,6 +1120,47 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
expect
(
model
.
options
.
uniqueKeys
[
fk
].
fields
).
to
.
deep
.
equal
([
'TaskId'
,
'UserId'
])
expect
(
model
.
options
.
uniqueKeys
[
fk
].
fields
).
to
.
deep
.
equal
([
'TaskId'
,
'UserId'
])
})
})
})
})
describe
(
'without sync'
,
function
()
{
beforeEach
(
function
(
done
)
{
var
self
=
this
self
.
sequelize
.
queryInterface
.
createTable
(
'users'
,{
id
:
{
type
:
DataTypes
.
INTEGER
,
primaryKey
:
true
,
autoIncrement
:
true
}
,
username
:
DataTypes
.
STRING
,
createdAt
:
DataTypes
.
DATE
,
updatedAt
:
DataTypes
.
DATE
}).
success
(
function
()
{
self
.
sequelize
.
queryInterface
.
createTable
(
'tasks'
,{
id
:
{
type
:
DataTypes
.
INTEGER
,
primaryKey
:
true
,
autoIncrement
:
true
},
title
:
DataTypes
.
STRING
,
createdAt
:
DataTypes
.
DATE
,
updatedAt
:
DataTypes
.
DATE
}).
success
(
function
()
{
self
.
sequelize
.
queryInterface
.
createTable
(
'users_tasks'
,{
TaskId
:
DataTypes
.
INTEGER
,
UserId
:
DataTypes
.
INTEGER
,
createdAt
:
DataTypes
.
DATE
,
updatedAt
:
DataTypes
.
DATE
}).
success
(
function
()
{
done
();
})
})
})
})
it
(
'removes all associations'
,
function
(
done
)
{
var
self
=
this
;
this
.
UsersTasks
=
this
.
sequelize
.
define
(
'UsersTasks'
,
{},
{
tableName
:
'users_tasks'
});
self
.
User
.
hasMany
(
self
.
Task
,
{
through
:
this
.
UsersTasks
})
self
.
Task
.
hasMany
(
self
.
User
,
{
through
:
this
.
UsersTasks
})
expect
(
Object
.
keys
(
self
.
UsersTasks
.
primaryKeys
)).
to
.
deep
.
equal
([
'TaskId'
,
'UserId'
])
self
.
User
.
create
({
username
:
'foo'
}).
success
(
function
(
user
)
{
self
.
Task
.
create
({
title
:
'foo'
}).
success
(
function
(
task
)
{
user
.
addTask
(
task
).
success
(
function
(){
user
.
setTasks
(
null
).
success
(
function
(
result
)
{
expect
(
result
).
to
.
be
.
ok
done
()
}).
error
(
function
(
error
)
{
console
.
log
(
error
);
expect
(
false
).
to
.
be
.
ok
done
()
})
})
})
})
})
})
})
})
describe
(
'through'
,
function
()
{
describe
(
'through'
,
function
()
{
...
...
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