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 a06a92d5
authored
Mar 03, 2016
by
Trey Thomas
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
MySQL: drop foreign key before dropping column
1 parent
489099fc
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
80 additions
and
1 deletions
lib/dialects/mysql/query-generator.js
lib/dialects/mysql/query-interface.js
lib/query-interface.js
lib/dialects/mysql/query-generator.js
View file @
a06a92d
...
...
@@ -332,6 +332,26 @@ var QueryGenerator = {
},
/**
* Generates an SQL query that returns the foreign key constraint of a given column.
*
* @param {String} tableName The name of the table.
* @param {String} columnName The name of the column.
* @return {String} The generated sql query.
*/
getForeignKeyQuery
:
function
(
table
,
columnName
)
{
var
tableName
=
table
.
tableName
||
table
;
if
(
table
.
schema
)
{
tableName
=
table
.
schema
+
'.'
+
tableName
;
}
return
[
'SELECT CONSTRAINT_NAME as constraint_name'
,
'FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE'
,
'WHERE TABLE_NAME = '
+
wrapSingleQuote
(
tableName
),
'AND COLUMN_NAME = '
+
wrapSingleQuote
(
columnName
),
].
join
(
' '
);
},
/**
* Generates an SQL query that removes a foreign key from a table.
*
* @param {String} tableName The name of the table.
...
...
@@ -343,4 +363,9 @@ var QueryGenerator = {
}
};
// private methods
function
wrapSingleQuote
(
identifier
){
return
Utils
.
addTicks
(
identifier
,
'\''
);
}
module
.
exports
=
Utils
.
_
.
extend
(
Utils
.
_
.
clone
(
require
(
'../abstract/query-generator'
)),
QueryGenerator
);
lib/dialects/mysql/query-interface.js
0 → 100644
View file @
a06a92d
'use strict'
;
/**
Returns an object that treats MySQL's inabilities to do certain queries.
@class QueryInterface
@static
*/
var
_
=
require
(
'lodash'
);
/**
A wrapper that fixes MySQL's inability to cleanly remove columns from existing tables if they have a foreign key constraint.
@method removeColumn
@for QueryInterface
@param {String} tableName The name of the table.
@param {String} columnName The name of the attribute that we want to remove.
@param {Object} options
*/
var
removeColumn
=
function
(
tableName
,
columnName
,
options
)
{
var
self
=
this
;
options
=
options
||
{};
return
self
.
sequelize
.
query
(
self
.
QueryGenerator
.
getForeignKeyQuery
(
tableName
,
columnName
),
_
.
assign
({
raw
:
true
},
options
)
)
.
spread
(
function
(
results
)
{
if
(
!
results
.
length
)
{
// No foreign key constraints found, so we can remove the column
return
;
}
return
self
.
sequelize
.
query
(
self
.
QueryGenerator
.
dropForeignKeyQuery
(
tableName
,
results
[
0
].
constraint_name
),
_
.
assign
({
raw
:
true
},
options
)
);
})
.
then
(
function
()
{
return
self
.
sequelize
.
query
(
self
.
QueryGenerator
.
removeColumnQuery
(
tableName
,
columnName
),
_
.
assign
({
raw
:
true
},
options
)
);
});
};
module
.
exports
=
{
removeColumn
:
removeColumn
};
lib/query-interface.js
View file @
a06a92d
...
...
@@ -5,6 +5,7 @@ var Utils = require('./utils')
,
DataTypes
=
require
(
'./data-types'
)
,
SQLiteQueryInterface
=
require
(
'./dialects/sqlite/query-interface'
)
,
MSSSQLQueryInterface
=
require
(
'./dialects/mssql/query-interface'
)
,
MySQLQueryInterface
=
require
(
'./dialects/mysql/query-interface'
)
,
Transaction
=
require
(
'./transaction'
)
,
Promise
=
require
(
'./promise'
)
,
QueryTypes
=
require
(
'./query-types'
);
...
...
@@ -360,8 +361,11 @@ QueryInterface.prototype.removeColumn = function(tableName, attributeName, optio
// sqlite needs some special treatment as it cannot drop a column
return
SQLiteQueryInterface
.
removeColumn
.
call
(
this
,
tableName
,
attributeName
,
options
);
case
'mssql'
:
// mssql needs special treatment as it cannot drop a column with a default constraint
// mssql needs special treatment as it cannot drop a column with a default
or foreign key
constraint
return
MSSSQLQueryInterface
.
removeColumn
.
call
(
this
,
tableName
,
attributeName
,
options
);
case
'mysql'
:
// mysql needs special treatment as it cannot drop a column with a foreign key constraint
return
MySQLQueryInterface
.
removeColumn
.
call
(
this
,
tableName
,
attributeName
,
options
);
default
:
var
sql
=
this
.
QueryGenerator
.
removeColumnQuery
(
tableName
,
attributeName
);
return
this
.
sequelize
.
query
(
sql
,
options
);
...
...
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