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

Commit 2d21c8c0 by Sascha Depold

use the assocname instead of the tablename for many to many tables

1 parent 07959e96
...@@ -14,8 +14,11 @@ exports.Helper = function(Sequelize) { ...@@ -14,8 +14,11 @@ exports.Helper = function(Sequelize) {
}, },
SQL: { SQL: {
manyToManyTableName: function(t1, t2) { manyToManyTableName: function(name1, name2) {
return [t1.tableName, t2.tableName].sort().join("") var _name1 = name1[0].toUpperCase() + name1.replace(/^./, "")
var _name2 = name2[0].toUpperCase() + name2.replace(/^./, "")
return [_name1, _name2].sort().join("")
}, },
asTableIdentifier: function(name) { asTableIdentifier: function(name) {
......
...@@ -72,7 +72,7 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o ...@@ -72,7 +72,7 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o
var _attributes = {} var _attributes = {}
_attributes[table.identifier] = Sequelize.INTEGER _attributes[table.identifier] = Sequelize.INTEGER
_attributes[association.table.identifier] = Sequelize.INTEGER _attributes[association.table.identifier] = Sequelize.INTEGER
sequelize.define(Sequelize.Helper.SQL.manyToManyTableName(table, association.table), _attributes) sequelize.define(Sequelize.Helper.SQL.manyToManyTableName(table.tableName, association.name), _attributes)
} else { } else {
// one to many relation // one to many relation
association.table.attributes[table.identifier] = {type: Sequelize.INTEGER} association.table.attributes[table.identifier] = {type: Sequelize.INTEGER}
...@@ -159,7 +159,7 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o ...@@ -159,7 +159,7 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o
// don't check inside of method to increase performance // don't check inside of method to increase performance
if(_table.isCrossAssociatedWith(table)) { if(_table.isCrossAssociatedWith(table)) {
table.prototype[assocName] = function(callback) { table.prototype[assocName] = function(callback) {
var Association = sequelize.tables[Sequelize.Helper.SQL.manyToManyTableName(_table, table)].klass var Association = sequelize.tables[Sequelize.Helper.SQL.manyToManyTableName(assocName, table.tableName)].klass
var whereConditions = [table.identifier, this.id].join("=") var whereConditions = [table.identifier, this.id].join("=")
Association.findAll({ where: whereConditions }, function(result) { Association.findAll({ where: whereConditions }, function(result) {
if(result.length > 0) { if(result.length > 0) {
...@@ -173,7 +173,7 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o ...@@ -173,7 +173,7 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o
} }
table.prototype[Sequelize.Helper.SQL.addPrefix('set', assocName)] = function(objects, callback) { table.prototype[Sequelize.Helper.SQL.addPrefix('set', assocName)] = function(objects, callback) {
var self = this var self = this
var Association = sequelize.tables[Sequelize.Helper.SQL.manyToManyTableName(_table, table)].klass var Association = sequelize.tables[Sequelize.Helper.SQL.manyToManyTableName(assocName, table.tableName)].klass
var currentAssociations = null var currentAssociations = null
var objectIds = Sequelize.Helper.Array.map(objects, function(obj) { return obj.id }) var objectIds = Sequelize.Helper.Array.map(objects, function(obj) { return obj.id })
......
...@@ -13,8 +13,8 @@ module.exports = { ...@@ -13,8 +13,8 @@ module.exports = {
}, },
'SQL: manyToManyTableName': function(assert) { 'SQL: manyToManyTableName': function(assert) {
assert.equal(h.SQL.manyToManyTableName({tableName: 'foo'}, {tableName: 'bar'}), 'barfoo') assert.equal(h.SQL.manyToManyTableName('foo', 'bar'), 'BarFoo')
assert.equal(h.SQL.manyToManyTableName({tableName: 'bar'}, {tableName: 'foo'}), 'barfoo') assert.equal(h.SQL.manyToManyTableName('bar','foo'), 'BarFoo')
}, },
'SQL: asTableIdentifier': function(assert) { 'SQL: asTableIdentifier': function(assert) {
assert.equal(h.SQL.asTableIdentifier('Users'), 'userId') assert.equal(h.SQL.asTableIdentifier('Users'), 'userId')
......
...@@ -51,8 +51,8 @@ module.exports = { ...@@ -51,8 +51,8 @@ module.exports = {
'prepareAssociations hasMany': function(assert) { 'prepareAssociations hasMany': function(assert) {
var ManyToManyPart1 = s.define('ManyToManyPart1', {}) var ManyToManyPart1 = s.define('ManyToManyPart1', {})
var ManyToManyPart2 = s.define('ManyToManyPart2', {}) var ManyToManyPart2 = s.define('ManyToManyPart2', {})
ManyToManyPart1.hasMany('manyToManyPart1', ManyToManyPart2) ManyToManyPart1.hasMany('manyToManyPart2s', ManyToManyPart2)
ManyToManyPart2.hasMany('manyToManyPart1', ManyToManyPart1) ManyToManyPart2.hasMany('manyToManyPart1s', ManyToManyPart1)
ManyToManyPart1.prepareAssociations() ManyToManyPart1.prepareAssociations()
ManyToManyPart2.prepareAssociations() ManyToManyPart2.prepareAssociations()
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!