不要怂,就是干,撸起袖子干!

Commit d51cc90e by Jan Aagaard Meier

Fix indentation in formatError and fix docs for findOrCreate. Use .get instead o…

…f direct access in findOrCreate
1 parent 023bab43
......@@ -52,18 +52,19 @@ module.exports = (function() {
var match;
switch (err.errno || err.code) {
case 1062:
match = err.message.match(/Duplicate entry '(.*)' for key '(.*?)'$/);
return new sequelizeErrors.UniqueConstraintError({
fields: null,
index: match[2],
value: match[1],
parent: err
});
case 1062:
match = err.message.match(/Duplicate entry '(.*)' for key '(.*?)'$/);
return new sequelizeErrors.UniqueConstraintError({
fields: null,
index: match[2],
value: match[1],
parent: err
});
default:
return new sequelizeErrors.DatabaseError(err);
}
return new sequelizeErrors.DatabaseError(err);
};
Query.prototype.isShowIndexesQuery = function () {
......
......@@ -226,18 +226,19 @@ module.exports = (function() {
var match;
switch (err.code) {
case '23505':
match = err.detail.match(/Key \((.*?)\)=\((.*?)\) already exists/);
return new sequelizeErrors.UniqueConstraintError({
fields: match[1].split(', '),
value: match[2].split(', '),
index: null,
parent: err
});
}
case '23505':
match = err.detail.match(/Key \((.*?)\)=\((.*?)\) already exists/);
return new sequelizeErrors.UniqueConstraintError({
fields: match[1].split(', '),
value: match[2].split(', '),
index: null,
parent: err
});
return new sequelizeErrors.DatabaseError(err);
default:
return new sequelizeErrors.DatabaseError(err);
}
};
Query.prototype.isShowIndexesQuery = function () {
......
......@@ -177,32 +177,33 @@ module.exports = (function() {
var match;
switch (err.code) {
case 'SQLITE_CONSTRAINT':
match = err.message.match(/columns (.*?) are/); // Sqlite pre 2.2 behavior - Error: SQLITE_CONSTRAINT: columns x, y are not unique
if (match !== null && match.length >= 2) {
return new sequelizeErrors.UniqueConstraintError(match[1].split(', '),null, err);
}
match = err.message.match(/UNIQUE constraint failed: (.*)/); // Sqlite 2.2 behavior - Error: SQLITE_CONSTRAINT: UNIQUE constraint failed: table.x, table.y
if (match !== null && match.length >= 2) {
var fields = match[1].split(', ').map(function (columnWithTable) {
return columnWithTable.split('.')[1];
});
case 'SQLITE_CONSTRAINT':
match = err.message.match(/columns (.*?) are/); // Sqlite pre 2.2 behavior - Error: SQLITE_CONSTRAINT: columns x, y are not unique
if (match !== null && match.length >= 2) {
return new sequelizeErrors.UniqueConstraintError(match[1].split(', '),null, err);
}
return new sequelizeErrors.UniqueConstraintError({
fields: fields,
index: null,
value: null,
parent: err
});
}
match = err.message.match(/UNIQUE constraint failed: (.*)/); // Sqlite 2.2 behavior - Error: SQLITE_CONSTRAINT: UNIQUE constraint failed: table.x, table.y
if (match !== null && match.length >= 2) {
var fields = match[1].split(', ').map(function (columnWithTable) {
return columnWithTable.split('.')[1];
});
return new sequelizeErrors.UniqueConstraintError({
fields: fields,
index: null,
value: null,
parent: err
});
}
return err;
case 'SQLITE_BUSY':
return new sequelizeErrors.TimeoutError(err);
}
return err;
case 'SQLITE_BUSY':
return new sequelizeErrors.TimeoutError(err);
return new sequelizeErrors.DatabaseError(err);
default:
return new sequelizeErrors.DatabaseError(err);
}
};
Query.prototype.handleShowIndexesQuery = function (data) {
......
......@@ -1081,8 +1081,8 @@ module.exports = (function() {
* However, it is not always possible to handle this case in SQLite, specifically if one transaction inserts and another tries to select before the first one has comitted. In this case, an instance of sequelize.TimeoutError will be thrown instead.
* If a transaction is created, a savepoint will be created instead, and any unique constraint violation will be handled internally.
*
* @param {Object} [options]
* @param {Object} [options.where] where A hash of search attributes. Note that this method differs from finders, in that the syntax is `{ attr1: 42 }` and NOT `{ where: { attr1: 42}}`. This is subject to change in 2.0
* @param {Object} options
* @param {Object} options.where where A hash of search attributes.
* @param {Object} [options.defaults] Default values to use if creating a new instance
* @param {Object} [queryOptions] Options passed to the find and create calls
*
......@@ -1095,8 +1095,8 @@ module.exports = (function() {
queryOptions = queryOptions ? Utils._.clone(queryOptions) : {};
if (!options.where) {
throw new Error('Missing where attribute in the first parameter passed to findOrCreate. Please note that the API has changed, and is now options (an object with where and defaults keys), queryOptions (transaction etc.)');
if (!options || !options.where) {
throw new Error('Missing where attribute in the options parameter passed to findOrCreate. Please note that the API has changed, and is now options (an object with where and defaults keys), queryOptions (transaction etc.)');
}
// Create a transaction or a savepoint, depending on whether a transaction was passed in
......@@ -1123,7 +1123,7 @@ module.exports = (function() {
queryOptions.exception = 'WHEN unique_violation THEN RETURN QUERY SELECT * FROM <%= table %> WHERE 1 <> 1;';
return self.create(values, queryOptions).bind(this).then(function(instance) {
if (instance[self.primaryKeyAttribute] === null) {
if (instance.get(self.primaryKeyAttribute, { raw: true }) === null) {
// If the query returned an empty result for the primary key, we know that this was actually a unique constraint violation
throw new self.sequelize.UniqueConstraintError();
}
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!