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

Commit 16ebcdd7 by Joel Trost Committed by Matt Broadstone

Bug fix for field on Sequelize function

1 parent e7bf0570
...@@ -161,13 +161,13 @@ module.exports = (function() { ...@@ -161,13 +161,13 @@ module.exports = (function() {
for(var key in attributes){ for(var key in attributes){
var aliasKey = attributes[key].field || key; var aliasKey = attributes[key].field || key;
if(ignoreKeys.indexOf(aliasKey) < 0){ if(ignoreKeys.indexOf(aliasKey) < 0){
ignoreKeys.push(aliasKey); ignoreKeys.push(aliasKey);
} }
if(attributes[key].autoIncrement){ if(attributes[key].autoIncrement){
for(var i = 0; i < attrValueHashes.length; i++){ for(var i = 0; i < attrValueHashes.length; i++){
if(aliasKey in attrValueHashes[i]){ if(aliasKey in attrValueHashes[i]){
delete attrValueHashes[i][aliasKey]; delete attrValueHashes[i][aliasKey];
} }
} }
} }
...@@ -216,32 +216,33 @@ module.exports = (function() { ...@@ -216,32 +216,33 @@ module.exports = (function() {
If you use a string, you have to escape it on your own. If you use a string, you have to escape it on your own.
*/ */
updateQuery: function(tableName, attrValueHash, where, options, attributes) { updateQuery: function(tableName, attrValueHash, where, options, attributes) {
// if(where){ var query;
// throw new Error();
// }
//console.log('here', where);
attrValueHash = Utils.removeNullValuesFromHash(attrValueHash, false, options); attrValueHash = Utils.removeNullValuesFromHash(attrValueHash, false, options);
for(var key in attributes){ //very unique case for cascades, i generally don't approve
//console.log(key); if(Object.keys(attrValueHash).length === 1 && attributes[Object.keys(attrValueHash)[0]].primaryKey){
//console.log('some more action', attributes.deletedAtThisTime); console.warn('Updating a Primary Key is not supported in MSSQL, please restructure your query');
var aliasKey = attributes[key].field || key; }else{
if(attributes[key].primaryKey && attrValueHash[aliasKey]){ for(var key in attributes){
delete attrValueHash[aliasKey]; var aliasKey = attributes[key].field || key;
} if(attributes[key].primaryKey && attrValueHash[aliasKey]){
if(attrValueHash[aliasKey] && attrValueHash[aliasKey].fn){ delete attrValueHash[aliasKey];
}
if(attrValueHash[aliasKey] && attrValueHash[aliasKey].fn){
}
} }
if(!Object.keys(attrValueHash).length){
return '';
//return ['SELECT * FROM ', tableName, 'WHERE', this.getWhereConditions(where) + ';'].join(' ');
}
query = [
SqlGenerator.updateSql(tableName, attrValueHash, attributes),
'WHERE',
this.getWhereConditions(where)
].join(' ') + ';';
} }
if(!Object.keys(attrValueHash).length){
return '';
//return ['SELECT * FROM ', tableName, 'WHERE', this.getWhereConditions(where) + ';'].join(' ');
}
var query = [
SqlGenerator.updateSql(tableName, attrValueHash, attributes),
'WHERE',
this.getWhereConditions(where)
].join(' ') + ';';
return query; return query;
}, },
/* /*
...@@ -285,8 +286,6 @@ module.exports = (function() { ...@@ -285,8 +286,6 @@ module.exports = (function() {
attrValueHash = Utils.removeNullValuesFromHash(attrValueHash, false, options); attrValueHash = Utils.removeNullValuesFromHash(attrValueHash, false, options);
for(var key in attributes){ for(var key in attributes){
//console.log(key);
//console.log('some more action', attributes.deletedAtThisTime);
if(attributes[key].primaryKey && attrValueHash[key]){ if(attributes[key].primaryKey && attrValueHash[key]){
delete attrValueHash[key]; delete attrValueHash[key];
} }
...@@ -373,7 +372,6 @@ module.exports = (function() { ...@@ -373,7 +372,6 @@ module.exports = (function() {
for(key in attributes) { for(key in attributes) {
attribute = attributes[key]; attribute = attributes[key];
//console.log(attribute);
if(key && !attribute.field) if(key && !attribute.field)
attribute.field = key; attribute.field = key;
result[attribute.field || key] = SqlGenerator.attributeToSQL(attribute, options); result[attribute.field || key] = SqlGenerator.attributeToSQL(attribute, options);
...@@ -586,27 +584,6 @@ module.exports = (function() { ...@@ -586,27 +584,6 @@ module.exports = (function() {
selectQuery: function(tableName, options, model) { selectQuery: function(tableName, options, model) {
// Enter and change at your own peril -- Mick Hansen // Enter and change at your own peril -- Mick Hansen
// options = options || {};
// var query =[];
// if(options.attributes[0][0].fn === 'COUNT'){
// query.push(SqlGenerator.getCountClause('COUNT', options.attributes[0][0].args[0].col));
// }else{
// query.push(SqlGenerator.getSelectorClause(model,options));
// }
// query.push(SqlGenerator.getFromClause(model.tableName, model.name));
// if(options.include){
// for(var i = 0; i < options.include.length; i ++){
// query.push(SqlGenerator.getJoinClause(model, options.include[i]));
// }
// }
// if(options.hasOwnProperty('where')){
// query.push('WHERE');
// query.push(this.getWhereConditions(options.where, model.name, model, options));
// }
// //console.log(query.join(' ') + ';');
// return query.join(' ') + ';';
// Enter and change at your own peril -- Mick Hansen
options = options || {}; options = options || {};
var table = null var table = null
...@@ -991,7 +968,6 @@ module.exports = (function() { ...@@ -991,7 +968,6 @@ module.exports = (function() {
//var limitOrder = this.addLimitAndOffset(options, query); //var limitOrder = this.addLimitAndOffset(options, query);
// Add LIMIT, OFFSET to sub or main query // Add LIMIT, OFFSET to sub or main query
//console.log(mainQueryItems);
if (options.offset) { if (options.offset) {
//needs an order column for this to work //needs an order column for this to work
//default to id //default to id
...@@ -1143,6 +1119,7 @@ module.exports = (function() { ...@@ -1143,6 +1119,7 @@ module.exports = (function() {
} }
} }
options = options || {}; options = options || {};
if (typeof prepend === 'undefined') { if (typeof prepend === 'undefined') {
...@@ -1213,7 +1190,7 @@ module.exports = (function() { ...@@ -1213,7 +1190,7 @@ module.exports = (function() {
if (smth.attribute._isSequelizeMethod) { if (smth.attribute._isSequelizeMethod) {
key = this.getWhereConditions(smth.attribute, tableName, factory, options, prepend); key = this.getWhereConditions(smth.attribute, tableName, factory, options, prepend);
} else { } else {
key = SqlGenerator.quoteIdentifier(smth.attribute.Model.name) + '.' + SqlGenerator.quoteIdentifier(smth.attribute.fieldName); key = SqlGenerator.quoteIdentifier(smth.attribute.Model.name) + '.' + SqlGenerator.quoteIdentifier(smth.attribute.field);
} }
if (value._isSequelizeMethod) { if (value._isSequelizeMethod) {
...@@ -1348,7 +1325,6 @@ module.exports = (function() { ...@@ -1348,7 +1325,6 @@ module.exports = (function() {
var result = []; var result = [];
options = options || {}; options = options || {};
// Closures are nice // Closures are nice
Utils._.each(hash, function(value, key) { Utils._.each(hash, function(value, key) {
var _key var _key
......
...@@ -167,6 +167,7 @@ module.exports = (function() { ...@@ -167,6 +167,7 @@ module.exports = (function() {
}); });
} }
match = err.message.match(/Failed on step '(.*)'.Could not create constraint. See previous errors./); match = err.message.match(/Failed on step '(.*)'.Could not create constraint. See previous errors./);
match = err.message.match(/The DELETE statement conflicted with the REFERENCE constraint "(.*)". The conflict occurred in database "(.*)", table "(.*)", column '(.*)'./);
if(match && match.length > 0){ if(match && match.length > 0){
return new sequelizeErrors.ForeignKeyConstraintError({ return new sequelizeErrors.ForeignKeyConstraintError({
fields: null, fields: null,
......
...@@ -361,7 +361,7 @@ module.exports = { ...@@ -361,7 +361,7 @@ module.exports = {
var selFields = []; var selFields = [];
var insertKey = false; var insertKey = false;
for (var key in modelAttributeMap) { for (var key in modelAttributeMap) {
selFields.push('INSERTED.' + quoteIdentifier(modelAttributeMap[key].field)); selFields.push('INSERTED.' + quoteIdentifier( (modelAttributeMap[key].field || key)));
if(modelAttributeMap[key].autoIncrement if(modelAttributeMap[key].autoIncrement
&& (valueHash[key] === 'DEFAULT' && (valueHash[key] === 'DEFAULT'
|| valueHash[key] === null)){ || valueHash[key] === null)){
...@@ -396,7 +396,7 @@ module.exports = { ...@@ -396,7 +396,7 @@ module.exports = {
, values = []; , values = [];
query = 'UPDATE <%= tableName %> SET <%= values %> OUTPUT <%= selFields %>'; query = 'UPDATE <%= tableName %> SET <%= values %> OUTPUT <%= selFields %>';
var parsedValues = valuesToSql(valueHash, attributes, true); var parsedValues = valuesToSql(valueHash, attributes, true);
for (var key in valueHash) { for (var key in valueHash) {
var value = valueHash[key]; var value = valueHash[key];
......
...@@ -20,6 +20,7 @@ var sortById = function(a, b) { ...@@ -20,6 +20,7 @@ var sortById = function(a, b) {
describe(Support.getTestDialectTeaser("Include"), function () { describe(Support.getTestDialectTeaser("Include"), function () {
describe('findAll', function () { describe('findAll', function () {
this.timeout(30000);
beforeEach(function () { beforeEach(function () {
this.fixtureA = function(done) { this.fixtureA = function(done) {
var User = this.sequelize.define('User', {}) var User = this.sequelize.define('User', {})
...@@ -228,6 +229,7 @@ describe(Support.getTestDialectTeaser("Include"), function () { ...@@ -228,6 +229,7 @@ describe(Support.getTestDialectTeaser("Include"), function () {
}, callback) }, callback)
}, },
function (err) { function (err) {
console.log('err', err);
expect(err).not.to.be.ok expect(err).not.to.be.ok
done() done()
} }
......
...@@ -17,6 +17,7 @@ var sortById = function(a, b) { ...@@ -17,6 +17,7 @@ var sortById = function(a, b) {
describe(Support.getTestDialectTeaser("Includes with schemas"), function () { describe(Support.getTestDialectTeaser("Includes with schemas"), function () {
describe('findAll', function () { describe('findAll', function () {
this.timeout(30000);
beforeEach(function () { beforeEach(function () {
var self = this var self = this
this.fixtureA = function(done) { this.fixtureA = function(done) {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!