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 d37b0ce0
authored
Mar 18, 2016
by
ming.deng
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix `addColumn` with reference not add reference in mysql
1 parent
55da7651
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
62 additions
and
2 deletions
changelog.md
lib/dialects/mysql/query-generator.js
test/unit/sql/add-column.test.js
changelog.md
View file @
d37b0ce
# Future
-
[
FIXED
]
`addColumn`
with reference in mysql
[
#5592
](
https://github.com/sequelize/sequelize/issues/5592
)
-
[
ADDED
]
rejectOnEmpty mode
[
#272
](
https://github.com/sequelize/sequelize/issues/272
)
[
#5480
]
(https://github.com/sequelize/sequelize/issues/5480)
-
[
ADDED
]
`beforeCount`
hook
[
#5209
](
https://github.com/sequelize/sequelize/pull/5209
)
-
[
ADDED
]
`validationFailed`
hook
[
#1626
](
https://github.com/sequelize/sequelize/issues/1626
)
...
...
lib/dialects/mysql/query-generator.js
View file @
d37b0ce
...
...
@@ -102,7 +102,8 @@ var QueryGenerator = {
,
attribute
=
Utils
.
_
.
template
(
'<%= key %> <%= definition %>'
)({
key
:
this
.
quoteIdentifier
(
key
),
definition
:
this
.
attributeToSQL
(
dataType
,
{
context
:
'addColumn'
context
:
'addColumn'
,
foreignKey
:
key
})
});
...
...
@@ -229,7 +230,7 @@ var QueryGenerator = {
return
Utils
.
_
.
template
(
sql
)({
tableName
:
this
.
quoteIdentifiers
(
tableName
),
indexName
:
indexName
});
},
attributeToSQL
:
function
(
attribute
)
{
attributeToSQL
:
function
(
attribute
,
options
)
{
if
(
!
Utils
.
_
.
isPlainObject
(
attribute
))
{
attribute
=
{
type
:
attribute
...
...
@@ -265,6 +266,16 @@ var QueryGenerator = {
if
(
attribute
.
references
)
{
attribute
=
Utils
.
formatReferences
(
attribute
);
if
(
options
&&
options
.
context
===
'addColumn'
&&
options
.
foreignKey
)
{
var
attrName
=
options
.
foreignKey
;
template
+=
Utils
.
_
.
template
(
', ADD CONSTRAINT <%= fkName %> FOREIGN KEY (<%= attrName %>)'
)({
fkName
:
this
.
quoteIdentifier
(
attrName
+
'_foreign_idx'
),
attrName
:
this
.
quoteIdentifier
(
attrName
)
});
}
template
+=
' REFERENCES '
+
this
.
quoteTable
(
attribute
.
references
.
model
);
if
(
attribute
.
references
.
key
)
{
...
...
test/unit/sql/add-column.test.js
0 → 100644
View file @
d37b0ce
'use strict'
;
/* jshint -W030, -W110 */
var
Support
=
require
(
__dirname
+
'/../support'
)
,
DataTypes
=
require
(
'../../../lib/data-types'
)
,
expectsql
=
Support
.
expectsql
,
current
=
Support
.
sequelize
,
sql
=
current
.
dialect
.
QueryGenerator
;
if
(
current
.
dialect
.
name
===
'mysql'
)
{
describe
(
Support
.
getTestDialectTeaser
(
'SQL'
),
function
()
{
describe
(
'addColumn'
,
function
()
{
var
Model
=
current
.
define
(
'users'
,
{
id
:
{
type
:
DataTypes
.
INTEGER
,
primaryKey
:
true
,
autoIncrement
:
true
}
},
{
timestamps
:
false
});
it
(
'properly generate alter queries'
,
function
(){
return
expectsql
(
sql
.
addColumnQuery
(
Model
.
getTableName
(),
'level_id'
,
current
.
normalizeAttribute
({
type
:
DataTypes
.
FLOAT
,
allowNull
:
false
,
})),
{
mysql
:
'ALTER TABLE `users` ADD `level_id` FLOAT NOT NULL;'
,
});
});
it
(
'properly generate alter queries for foreign keys'
,
function
(){
return
expectsql
(
sql
.
addColumnQuery
(
Model
.
getTableName
(),
'level_id'
,
current
.
normalizeAttribute
({
type
:
DataTypes
.
INTEGER
,
references
:
{
model
:
'level'
,
key
:
'id'
},
onUpdate
:
'cascade'
,
onDelete
:
'cascade'
})),
{
mysql
:
'ALTER TABLE `users` ADD `level_id` INTEGER, ADD CONSTRAINT `level_id_foreign_idx` FOREIGN KEY (`level_id`) REFERENCES `level` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;'
,
});
});
});
});
}
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