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 489099fc
authored
Mar 02, 2016
by
Trey Thomas
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
MSSQL: drop foreign key before dropping column
1 parent
f3327436
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
34 additions
and
1 deletions
lib/dialects/mssql/query-generator.js
lib/dialects/mssql/query-interface.js
lib/dialects/mssql/query-generator.js
View file @
489099f
...
...
@@ -507,7 +507,7 @@ var QueryGenerator = {
return
'['
+
identifier
.
replace
(
/
[\[\]
'
]
+/g
,
''
)
+
']'
;
},
getForeignKeysQuery
:
function
(
table
,
databaseName
)
{
getForeignKeysQuery
:
function
(
table
)
{
var
tableName
=
table
.
tableName
||
table
;
var
sql
=
[
'SELECT'
,
...
...
@@ -525,6 +525,27 @@ var QueryGenerator = {
return
sql
;
},
getForeignKeyQuery
:
function
(
table
,
attributeName
)
{
var
tableName
=
table
.
tableName
||
table
;
var
sql
=
[
'SELECT'
,
'constraint_name = TC.CONSTRAINT_NAME'
,
'FROM'
,
'INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC'
,
'JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE CCU'
,
'ON TC.CONSTRAINT_NAME = CCU.CONSTRAINT_NAME'
,
"WHERE TC.CONSTRAINT_TYPE = 'FOREIGN KEY'"
,
'AND TC.TABLE_NAME ='
,
wrapSingleQuote
(
tableName
),
'AND CCU.COLUMN_NAME ='
,
wrapSingleQuote
(
attributeName
),
].
join
(
' '
);
if
(
table
.
schema
)
{
sql
+=
' AND TC.TABLE_SCHEMA ='
+
wrapSingleQuote
(
table
.
schema
);
}
return
sql
;
},
dropForeignKeyQuery
:
function
(
tableName
,
foreignKey
)
{
return
Utils
.
_
.
template
(
'ALTER TABLE <%= table %> DROP <%= key %>'
)({
table
:
this
.
quoteTable
(
tableName
),
...
...
lib/dialects/mssql/query-interface.js
View file @
489099f
...
...
@@ -33,6 +33,18 @@ var removeColumn = function (tableName, attributeName, options) {
return
self
.
sequelize
.
query
(
dropConstraintSql
,
{
raw
:
true
,
logging
:
options
.
logging
});
})
.
then
(
function
()
{
var
findForeignKeySql
=
self
.
QueryGenerator
.
getForeignKeyQuery
(
tableName
,
attributeName
);
return
self
.
sequelize
.
query
(
findForeignKeySql
,
{
raw
:
true
,
logging
:
options
.
logging
});
})
.
spread
(
function
(
results
)
{
if
(
!
results
.
length
)
{
// No foreign key constraints found, so we can remove the column
return
;
}
var
dropForeignKeySql
=
self
.
QueryGenerator
.
dropForeignKeyQuery
(
tableName
,
results
[
0
].
constraint_name
);
return
self
.
sequelize
.
query
(
dropForeignKeySql
,
{
raw
:
true
,
logging
:
options
.
logging
});
})
.
then
(
function
()
{
var
removeSql
=
self
.
QueryGenerator
.
removeColumnQuery
(
tableName
,
attributeName
);
return
self
.
sequelize
.
query
(
removeSql
,
{
raw
:
true
,
logging
:
options
.
logging
});
});
...
...
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