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 4fe70b41
authored
Jun 01, 2015
by
John Giudici III
Committed by
John Giudici
Jun 15, 2015
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add support for unique constraint custom error message
1 parent
bebad6e2
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
58 additions
and
7 deletions
changelog.md
lib/dialects/abstract/query.js
lib/dialects/mssql/query.js
lib/dialects/mysql/query.js
lib/dialects/postgres/query.js
lib/dialects/sqlite/query.js
lib/model.js
test/integration/instance.validations.test.js
changelog.md
View file @
4fe70b4
# Next
-
[
FIXED
]
Fix
`Promise#nodeify()`
and
`Promise#done()`
not passing CLS context
-
[
ADDED
]
Unique constraints may now include custom error messages
# 3.2.0
-
[
FEATURE
]
Add support for new option
`targetKey`
in a belongs-to relationship for situations where the target key is not the id field.
...
...
lib/dialects/abstract/query.js
View file @
4fe70b4
...
...
@@ -414,6 +414,19 @@ AbstractQuery.prototype.findTableNameInAttribute = function(attribute) {
}
};
AbstractQuery
.
prototype
.
getUniqueConstraintErrorMessage
=
function
(
field
)
{
var
message
=
field
+
' must be unique'
;
var
self
=
this
;
Object
.
keys
(
self
.
model
.
uniqueKeys
).
forEach
(
function
(
key
)
{
if
(
self
.
model
.
uniqueKeys
[
key
].
fields
.
indexOf
(
field
.
replace
(
/"/g
,
''
))
>=
0
)
{
if
(
self
.
model
.
uniqueKeys
[
key
].
hasOwnProperty
(
'msg'
))
{
message
=
self
.
model
.
uniqueKeys
[
key
].
msg
;
}
}
});
return
message
;
};
AbstractQuery
.
prototype
.
isRawQuery
=
function
()
{
return
this
.
options
.
type
===
QueryTypes
.
RAW
;
};
...
...
lib/dialects/mssql/query.js
View file @
4fe70b4
...
...
@@ -185,9 +185,11 @@ Query.prototype.formatError = function (err) {
}
var
errors
=
[];
var
self
=
this
;
Utils
.
_
.
forOwn
(
fields
,
function
(
value
,
field
)
{
errors
.
push
(
new
sequelizeErrors
.
ValidationErrorItem
(
field
+
' must be unique'
,
'unique violation'
,
field
,
value
));
self
.
getUniqueConstraintErrorMessage
(
field
),
'unique violation'
,
field
,
value
));
});
return
new
sequelizeErrors
.
UniqueConstraintError
({
...
...
lib/dialects/mysql/query.js
View file @
4fe70b4
...
...
@@ -123,9 +123,11 @@ Query.prototype.formatError = function (err) {
}
var
errors
=
[];
var
self
=
this
;
Utils
.
_
.
forOwn
(
fields
,
function
(
value
,
field
)
{
errors
.
push
(
new
sequelizeErrors
.
ValidationErrorItem
(
field
+
' must be unique'
,
'unique violation'
,
field
,
value
));
self
.
getUniqueConstraintErrorMessage
(
field
),
'unique violation'
,
field
,
value
));
});
return
new
sequelizeErrors
.
UniqueConstraintError
({
...
...
lib/dialects/postgres/query.js
View file @
4fe70b4
...
...
@@ -134,10 +134,11 @@ Query.prototype.run = function(sql) {
if
(
rows
[
0
]
&&
rows
[
0
].
sequelize_caught_exception
!==
undefined
)
{
if
(
rows
[
0
].
sequelize_caught_exception
!==
null
)
{
throw
new
self
.
formatError
({
var
err
=
self
.
formatError
({
code
:
'23505'
,
detail
:
rows
[
0
].
sequelize_caught_exception
});
throw
err
;
}
else
{
rows
=
rows
.
map
(
function
(
row
)
{
delete
row
.
sequelize_caught_exception
;
...
...
@@ -343,7 +344,8 @@ Query.prototype.formatError = function (err) {
,
index
,
fields
,
errors
,
message
;
,
message
,
self
=
this
;
var
code
=
err
.
code
||
err
.
sqlState
,
errMessage
=
err
.
message
||
err
.
messagePrimary
...
...
@@ -374,7 +376,8 @@ Query.prototype.formatError = function (err) {
Utils
.
_
.
forOwn
(
fields
,
function
(
value
,
field
)
{
errors
.
push
(
new
sequelizeErrors
.
ValidationErrorItem
(
field
+
' must be unique'
,
'unique violation'
,
field
,
value
));
self
.
getUniqueConstraintErrorMessage
(
field
),
'unique violation'
,
field
,
value
));
});
if
(
this
.
model
&&
this
.
model
.
uniqueKeys
)
{
...
...
lib/dialects/sqlite/query.js
View file @
4fe70b4
...
...
@@ -215,7 +215,8 @@ Query.prototype.formatError = function (err) {
fields
.
forEach
(
function
(
field
)
{
errors
.
push
(
new
sequelizeErrors
.
ValidationErrorItem
(
field
+
' must be unique'
,
'unique violation'
,
field
,
self
.
instance
&&
self
.
instance
[
field
]));
self
.
getUniqueConstraintErrorMessage
(
field
),
'unique violation'
,
field
,
self
.
instance
&&
self
.
instance
[
field
]));
});
if
(
this
.
model
)
{
...
...
lib/model.js
View file @
4fe70b4
...
...
@@ -1866,6 +1866,7 @@ Model.prototype.bulkCreate = function(records, options) {
}
// Insert all records at once
options
.
model
=
self
;
return
self
.
QueryInterface
.
bulkInsert
(
self
.
getTableName
(
options
),
records
,
options
,
attributes
).
then
(
function
(
results
)
{
if
(
Array
.
isArray
(
results
))
{
results
.
forEach
(
function
(
result
,
i
)
{
...
...
test/integration/instance.validations.test.js
View file @
4fe70b4
...
...
@@ -79,7 +79,7 @@ describe(Support.getTestDialectTeaser('InstanceValidator'), function() {
});
});
it
(
'should enforce a unque constraint'
,
function
()
{
it
(
'should enforce a un
i
que constraint'
,
function
()
{
var
Model
=
this
.
sequelize
.
define
(
'model'
,
{
uniqueName
:
{
type
:
Sequelize
.
STRING
,
unique
:
true
}
});
...
...
@@ -103,6 +103,34 @@ describe(Support.getTestDialectTeaser('InstanceValidator'), function() {
expect
(
err
.
errors
[
0
].
message
).
to
.
include
(
'must be unique'
);
});
});
it
(
'should allow a custom unique constraint error message'
,
function
()
{
var
Model
=
this
.
sequelize
.
define
(
'model'
,
{
uniqueName
:
{
type
:
Sequelize
.
STRING
,
unique
:
{
msg
:
'custom unique error message'
}
}
});
var
records
=
[
{
uniqueName
:
'unique name one'
},
{
uniqueName
:
'unique name two'
}
];
return
Model
.
sync
({
force
:
true
})
.
then
(
function
()
{
return
Model
.
create
(
records
[
0
]);
}).
then
(
function
(
instance
)
{
expect
(
instance
).
to
.
be
.
ok
;
return
Model
.
create
(
records
[
1
]);
}).
then
(
function
(
instance
)
{
expect
(
instance
).
to
.
be
.
ok
;
return
expect
(
Model
.
update
(
records
[
0
],
{
where
:
{
id
:
instance
.
id
}
})).
to
.
be
.
rejected
;
}).
then
(
function
(
err
)
{
expect
(
err
).
to
.
be
.
an
.
instanceOf
(
Error
);
expect
(
err
.
errors
).
to
.
have
.
length
(
1
);
expect
(
err
.
errors
[
0
].
path
).
to
.
include
(
'uniqueName'
);
expect
(
err
.
errors
[
0
].
message
).
to
.
equal
(
'custom unique error message'
);
});
});
});
describe
(
'#create'
,
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