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

Commit bfa9d65c by Sascha Depold

many-to-many associations are almost working

1 parent 8d8b491a
Showing with 7 additions and 11 deletions
...@@ -22,10 +22,12 @@ SequelizeTable = function(sequelize, tableName, attributes) { ...@@ -22,10 +22,12 @@ SequelizeTable = function(sequelize, tableName, attributes) {
attributes: attributes, attributes: attributes,
tableName: tableName, tableName: tableName,
isCrossAssociatedWith: function(_table) { isCrossAssociatedWith: function(associatedTable) {
var result = false var result = false
_table.associations.forEach(function(association) { var myTableName = table.tableName
if((association.table.tableName == table.tableName) && (association.type == 'hasMany'))
associatedTable.associations.forEach(function(association) {
if((association.table.tableName == myTableName) && (association.type == 'hasMany'))
result = true result = true
}) })
return result return result
...@@ -35,20 +37,16 @@ SequelizeTable = function(sequelize, tableName, attributes) { ...@@ -35,20 +37,16 @@ SequelizeTable = function(sequelize, tableName, attributes) {
table.associations.forEach(function(association) { table.associations.forEach(function(association) {
switch(association.type) { switch(association.type) {
case 'hasMany': case 'hasMany':
if(association.table.isCrossAssociatedWith(association.table)) { if(association.table.isCrossAssociatedWith(table)) {
// many to many relation // many to many relation
SequelizeHelper.log(association)
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
SequelizeHelper.log(table.tableName + association.table.tableName) sequelize.define([table.tableName, association.table.tableName].sort().join(""), _attributes)
sequelize.define(table.tableName + association.table.tableName, _attributes)
} else { } else {
// one to many relation // one to many relation
association.table.attributes[table.identifier] = Sequelize.INTEGER association.table.attributes[table.identifier] = Sequelize.INTEGER
} }
break break
case 'hasOne': case 'hasOne':
// e.g. assocTable.myTableId = Sequelize.INTEGER // e.g. assocTable.myTableId = Sequelize.INTEGER
...@@ -63,8 +61,6 @@ SequelizeTable = function(sequelize, tableName, attributes) { ...@@ -63,8 +61,6 @@ SequelizeTable = function(sequelize, tableName, attributes) {
}, },
sync: function(callback) { sync: function(callback) {
table.prepareAssociations()
var fields = ["id INT NOT NULL auto_increment PRIMARY KEY"] var fields = ["id INT NOT NULL auto_increment PRIMARY KEY"]
SequelizeHelper.Hash.forEach(table.attributes, function(type, name) { SequelizeHelper.Hash.forEach(table.attributes, function(type, name) {
fields.push(name + " " + type) fields.push(name + " " + type)
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!