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

Commit 8319ed59 by Sascha Depold

fixed bug in associations

1 parent dca14ce1
Showing with 19 additions and 2 deletions
/*
A.hasOne(B) => B.aId
A.belongsTo(B) => A.bId
A.hasMany(B) => B.aId
A.hasMany(B) + B.hasMany(A) => AB.aId + AB.bId
*/
SequelizeTable = function(sequelize, tableName, attributes) { SequelizeTable = function(sequelize, tableName, attributes) {
var table = function(values) { var table = function(values) {
var self = this var self = this
...@@ -24,13 +31,19 @@ SequelizeTable = function(sequelize, tableName, attributes) { ...@@ -24,13 +31,19 @@ SequelizeTable = function(sequelize, tableName, attributes) {
return result return result
}, },
sync: function(callback) { prepareAssociations: function() {
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(association.table)) {
// many to many relation // many to many relation
SequelizeHelper.log(association)
var _attributes = {}
_attributes[table.identifier] = Sequelize.INTEGER
_attributes[association.table.identifier] = Sequelize.INTEGER
SequelizeHelper.log(table.tableName + association.table.tableName)
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
...@@ -43,10 +56,14 @@ SequelizeTable = function(sequelize, tableName, attributes) { ...@@ -43,10 +56,14 @@ SequelizeTable = function(sequelize, tableName, attributes) {
break break
case 'belongsTo': case 'belongsTo':
// e.g. table.dayId = Sequelize.INTEGER // e.g. table.dayId = Sequelize.INTEGER
table.attributes[table.identifier] = Sequelize.INTEGER table.attributes[association.table.identifier] = Sequelize.INTEGER
break break
} }
}) })
},
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) {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!