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 23453399
authored
Jul 10, 2015
by
Jan Aagaard Meier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Re-calcualte primary keys when calling refreshAttributes. Fixed #3652
1 parent
7888734e
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
77 additions
and
45 deletions
lib/model.js
test/unit/model/removeAttribute.test.js
lib/model.js
View file @
2345339
...
@@ -174,7 +174,8 @@ var addDefaultAttributes = function() {
...
@@ -174,7 +174,8 @@ var addDefaultAttributes = function() {
,
head
=
{};
,
head
=
{};
// Add id if no primary key was manually added to definition
// Add id if no primary key was manually added to definition
if
(
!
Object
.
keys
(
this
.
primaryKeys
).
length
)
{
// Can't use this.primaryKeys here, since this function is called before PKs are identified
if
(
!
_
.
any
(
this
.
rawAttributes
,
'primaryKey'
))
{
head
=
{
head
=
{
id
:
{
id
:
{
type
:
new
DataTypes
.
INTEGER
(),
type
:
new
DataTypes
.
INTEGER
(),
...
@@ -580,7 +581,6 @@ Model.prototype.init = function(modelManager) {
...
@@ -580,7 +581,6 @@ Model.prototype.init = function(modelManager) {
this
.
modelManager
=
modelManager
;
this
.
modelManager
=
modelManager
;
this
.
primaryKeys
=
{};
this
.
primaryKeys
=
{};
self
.
options
.
uniqueKeys
=
{};
// Setup names of timestamp attributes
// Setup names of timestamp attributes
this
.
_timestampAttributes
=
{};
this
.
_timestampAttributes
=
{};
...
@@ -596,51 +596,8 @@ Model.prototype.init = function(modelManager) {
...
@@ -596,51 +596,8 @@ Model.prototype.init = function(modelManager) {
}
}
}
}
// Identify primary and unique attributes
Utils
.
_
.
each
(
this
.
rawAttributes
,
function
(
options
,
attribute
)
{
if
(
options
.
hasOwnProperty
(
'unique'
))
{
var
idxName
;
if
(
options
.
unique
===
true
)
{
idxName
=
self
.
tableName
+
'_'
+
attribute
+
'_unique'
;
self
.
options
.
uniqueKeys
[
idxName
]
=
{
name
:
idxName
,
fields
:
[
attribute
],
singleField
:
true
};
}
else
if
(
options
.
unique
!==
false
)
{
idxName
=
options
.
unique
;
if
(
typeof
options
.
unique
===
'object'
)
{
idxName
=
options
.
unique
.
name
;
}
self
.
options
.
uniqueKeys
[
idxName
]
=
self
.
options
.
uniqueKeys
[
idxName
]
||
{
fields
:
[],
msg
:
null
};
self
.
options
.
uniqueKeys
[
idxName
].
fields
.
push
(
options
.
field
||
attribute
);
self
.
options
.
uniqueKeys
[
idxName
].
msg
=
self
.
options
.
uniqueKeys
[
idxName
].
msg
||
options
.
unique
.
msg
||
null
;
self
.
options
.
uniqueKeys
[
idxName
].
name
=
idxName
||
false
;
}
}
if
(
options
.
primaryKey
===
true
)
{
self
.
primaryKeys
[
attribute
]
=
self
.
attributes
[
attribute
];
}
});
this
.
uniqueKeys
=
this
.
options
.
uniqueKeys
;
// Add head and tail default attributes (id, timestamps)
// Add head and tail default attributes (id, timestamps)
addDefaultAttributes
.
call
(
this
);
addOptionalClassMethods
.
call
(
this
);
addOptionalClassMethods
.
call
(
this
);
// Primary key convenience variables
this
.
primaryKeyAttributes
=
Object
.
keys
(
this
.
primaryKeys
);
this
.
primaryKeyAttribute
=
this
.
primaryKeyAttributes
[
0
];
this
.
primaryKeyField
=
this
.
rawAttributes
[
this
.
primaryKeyAttribute
].
field
||
this
.
primaryKeyAttribute
;
this
.
primaryKeyCount
=
this
.
primaryKeyAttributes
.
length
;
this
.
_hasPrimaryKeys
=
this
.
options
.
hasPrimaryKeys
=
this
.
hasPrimaryKeys
=
this
.
primaryKeyCount
>
0
;
this
.
_isPrimaryKey
=
Utils
.
_
.
memoize
(
function
(
key
)
{
return
self
.
primaryKeyAttributes
.
indexOf
(
key
)
!==
-
1
;
});
this
.
$scope
=
_
.
isPlainObject
(
this
.
options
.
defaultScope
)
?
this
.
options
.
defaultScope
:
{};
this
.
$scope
=
_
.
isPlainObject
(
this
.
options
.
defaultScope
)
?
this
.
options
.
defaultScope
:
{};
...
@@ -668,7 +625,10 @@ Model.prototype.init = function(modelManager) {
...
@@ -668,7 +625,10 @@ Model.prototype.init = function(modelManager) {
self
.
Instance
.
prototype
[
name
]
=
fct
;
self
.
Instance
.
prototype
[
name
]
=
fct
;
});
});
}
}
addDefaultAttributes
.
call
(
this
);
this
.
refreshAttributes
();
this
.
refreshAttributes
();
findAutoIncrementField
.
call
(
this
);
findAutoIncrementField
.
call
(
this
);
this
.
Instance
.
prototype
.
$Model
=
this
.
Instance
.
prototype
.
$Model
=
...
@@ -743,6 +703,9 @@ Model.prototype.refreshAttributes = function() {
...
@@ -743,6 +703,9 @@ Model.prototype.refreshAttributes = function() {
this
.
fieldRawAttributesMap
=
{};
this
.
fieldRawAttributesMap
=
{};
this
.
primaryKeys
=
{};
self
.
options
.
uniqueKeys
=
{};
Utils
.
_
.
each
(
this
.
rawAttributes
,
function
(
definition
,
name
)
{
Utils
.
_
.
each
(
this
.
rawAttributes
,
function
(
definition
,
name
)
{
definition
.
type
=
self
.
sequelize
.
normalizeDataType
(
definition
.
type
);
definition
.
type
=
self
.
sequelize
.
normalizeDataType
(
definition
.
type
);
...
@@ -754,6 +717,10 @@ Model.prototype.refreshAttributes = function() {
...
@@ -754,6 +717,10 @@ Model.prototype.refreshAttributes = function() {
definition
.
field
=
name
;
definition
.
field
=
name
;
}
}
if
(
definition
.
primaryKey
===
true
)
{
self
.
primaryKeys
[
name
]
=
definition
;
}
self
.
fieldRawAttributesMap
[
definition
.
field
]
=
definition
;
self
.
fieldRawAttributesMap
[
definition
.
field
]
=
definition
;
if
(
definition
.
type
instanceof
DataTypes
.
BOOLEAN
)
{
if
(
definition
.
type
instanceof
DataTypes
.
BOOLEAN
)
{
...
@@ -784,6 +751,28 @@ Model.prototype.refreshAttributes = function() {
...
@@ -784,6 +751,28 @@ Model.prototype.refreshAttributes = function() {
self
.
_defaultValues
[
name
]
=
Utils
.
_
.
partial
(
Utils
.
toDefaultValue
,
definition
.
defaultValue
);
self
.
_defaultValues
[
name
]
=
Utils
.
_
.
partial
(
Utils
.
toDefaultValue
,
definition
.
defaultValue
);
}
}
if
(
definition
.
hasOwnProperty
(
'unique'
))
{
var
idxName
;
if
(
definition
.
unique
===
true
)
{
idxName
=
self
.
tableName
+
'_'
+
name
+
'_unique'
;
self
.
options
.
uniqueKeys
[
idxName
]
=
{
name
:
idxName
,
fields
:
[
definition
.
field
],
singleField
:
true
};
}
else
if
(
definition
.
unique
!==
false
)
{
idxName
=
definition
.
unique
;
if
(
typeof
definition
.
unique
===
'object'
)
{
idxName
=
definition
.
unique
.
name
;
}
self
.
options
.
uniqueKeys
[
idxName
]
=
self
.
options
.
uniqueKeys
[
idxName
]
||
{
fields
:
[],
msg
:
null
};
self
.
options
.
uniqueKeys
[
idxName
].
fields
.
push
(
definition
.
field
);
self
.
options
.
uniqueKeys
[
idxName
].
msg
=
self
.
options
.
uniqueKeys
[
idxName
].
msg
||
definition
.
unique
.
msg
||
null
;
self
.
options
.
uniqueKeys
[
idxName
].
name
=
idxName
||
false
;
}
}
if
(
definition
.
hasOwnProperty
(
'validate'
))
{
if
(
definition
.
hasOwnProperty
(
'validate'
))
{
self
.
Instance
.
prototype
.
validators
[
name
]
=
definition
.
validate
;
self
.
Instance
.
prototype
.
validators
[
name
]
=
definition
.
validate
;
}
}
...
@@ -798,6 +787,8 @@ Model.prototype.refreshAttributes = function() {
...
@@ -798,6 +787,8 @@ Model.prototype.refreshAttributes = function() {
}
}
});
});
this
.
uniqueKeys
=
this
.
options
.
uniqueKeys
;
this
.
_hasBooleanAttributes
=
!!
this
.
_booleanAttributes
.
length
;
this
.
_hasBooleanAttributes
=
!!
this
.
_booleanAttributes
.
length
;
this
.
_isBooleanAttribute
=
Utils
.
_
.
memoize
(
function
(
key
)
{
this
.
_isBooleanAttribute
=
Utils
.
_
.
memoize
(
function
(
key
)
{
return
self
.
_booleanAttributes
.
indexOf
(
key
)
!==
-
1
;
return
self
.
_booleanAttributes
.
indexOf
(
key
)
!==
-
1
;
...
@@ -848,6 +839,20 @@ Model.prototype.refreshAttributes = function() {
...
@@ -848,6 +839,20 @@ Model.prototype.refreshAttributes = function() {
this
.
Instance
.
prototype
.
_isAttribute
=
Utils
.
_
.
memoize
(
function
(
key
)
{
this
.
Instance
.
prototype
.
_isAttribute
=
Utils
.
_
.
memoize
(
function
(
key
)
{
return
self
.
Instance
.
prototype
.
attributes
.
indexOf
(
key
)
!==
-
1
;
return
self
.
Instance
.
prototype
.
attributes
.
indexOf
(
key
)
!==
-
1
;
});
});
// Primary key convenience variables
this
.
primaryKeyAttributes
=
Object
.
keys
(
this
.
primaryKeys
);
this
.
primaryKeyAttribute
=
this
.
primaryKeyAttributes
[
0
];
if
(
this
.
primaryKeyAttribute
)
{
this
.
primaryKeyField
=
this
.
rawAttributes
[
this
.
primaryKeyAttribute
].
field
||
this
.
primaryKeyAttribute
;
}
this
.
primaryKeyCount
=
this
.
primaryKeyAttributes
.
length
;
this
.
_hasPrimaryKeys
=
this
.
options
.
hasPrimaryKeys
=
this
.
hasPrimaryKeys
=
this
.
primaryKeyCount
>
0
;
this
.
_isPrimaryKey
=
Utils
.
_
.
memoize
(
function
(
key
)
{
return
self
.
primaryKeyAttributes
.
indexOf
(
key
)
!==
-
1
;
});
};
};
/**
/**
...
...
test/unit/model/removeAttribute.test.js
0 → 100644
View file @
2345339
'use strict'
;
/* jshint -W030 */
var
chai
=
require
(
'chai'
)
,
expect
=
chai
.
expect
,
Support
=
require
(
__dirname
+
'/../support'
)
,
current
=
Support
.
sequelize
,
_
=
require
(
'lodash'
)
,
DataTypes
=
require
(
__dirname
+
'/../../../lib/data-types'
);
describe
(
Support
.
getTestDialectTeaser
(
'Model'
),
function
()
{
describe
(
'removeAttribute'
,
function
()
{
it
(
'should support removing the primary key'
,
function
()
{
var
Model
=
current
.
define
(
'm'
,
{
name
:
DataTypes
.
STRING
});
expect
(
Model
.
primaryKeyAttribute
).
not
.
to
.
be
.
undefined
;
expect
(
_
.
size
(
Model
.
primaryKeys
)).
to
.
equal
(
1
);
Model
.
removeAttribute
(
'id'
);
expect
(
Model
.
primaryKeyAttribute
).
to
.
be
.
undefined
;
expect
(
_
.
size
(
Model
.
primaryKeys
)).
to
.
equal
(
0
);
});
});
});
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