2016-03-21 62 views
0

我遇到了一個問題,我試圖關聯兩個具有一對多關係的模型。由於某種原因,儘管引用關係,但這個查詢仍然引發錯誤。SequelizeJS無法讀取未定義的屬性'getTableName'

這是我的錯誤信息:

Unhandled rejection TypeError: Cannot read property 'getTableName' of undefined 
    at generateJoinQueries (/Users/user/Desktop/Projects/node/project/node_modules/sequelize/lib/dialects/abstract/query-generator.js:1181:43) 

這是路線:

appRoutes.route('/settings') 

    .get(function(req, res, organization){ 
     models.DiscoverySource.findAll({ 
      where: { 
       organizationId: req.user.organizationId 
      }, 
      include: [{ 
       model: models.Organization, through: { attributes: ['organizationName', 'admin', 'discoverySource']} 
      }] 
     }).then(function(organization, discoverySource){ 
      res.render('pages/app/settings.hbs',{ 
       organization: organization, 
       discoverySource: discoverySource 
      }); 
     }) 

    }); 

DiscoverySource:

module.exports = function(sequelize, DataTypes) { 

var DiscoverySource = sequelize.define('discovery_source', { 
    discoverySourceId: { 
     type: DataTypes.INTEGER, 
     field: 'discovery_source_id', 
     autoIncrement: true, 
     primaryKey: true 
    }, 
    discoverySource: { 
     type: DataTypes.STRING, 
     field: 'discovery_source_name' 
    }, 
    organizationId: { 
     type: DataTypes.TEXT, 
     field: 'organization_id' 
    }, 
},{ 
    freezeTableName: true, 
    classMethods: { 
     associate: function(db) { 
      DiscoverySource.belongsTo(db.Organization, {foreignKey: 'organization_id'}); 
     }, 
    }, 
}); 
    return DiscoverySource; 
} 

組織:

module.exports = function(sequelize, DataTypes) { 

var Organization = sequelize.define('organization', { 
    organizationId: { 
     type: DataTypes.INTEGER, 
     field: 'organization_id', 
     autoIncrement: true, 
     primaryKey: true 
    }, 
    organizationName: { 
     type: DataTypes.STRING, 
     field: 'organization_name' 
    }, 
    admin: DataTypes.STRING 
},{ 
    freezeTableName: true, 
    classMethods: { 
     associate: function(db) { 
      Organization.hasMany(db.DiscoverySource, {foreignKey: 'organization_id'}); 
     }, 
    } 
}); 
    return Organization; 
} 
+0

[Sequelize關聯錯誤的可能重複無法讀取未定義屬性'getTableName'](http://stackoverflow.com/questions/35974514/sequelize-association-error-cannot-read-property-gettablename-of-undefined) – Milkncookiez

回答

1

看來,這是Sequelize Association Error Cannot read property 'getTableName' of undefined的副本。

但是,你需要重寫查詢看起來像:

models.DiscoverySource.findAll({ 
    attributes: ['discoverySource'], 
    where: { 
     organizationId: req.user.organizationId 
    }, 
    include: [{ 
     model: models.Organization, 
     attributes: ['organizationName', 'admin'] 
    }] 
}) 

按照Sequelize文檔:

[options.attributes] - 要在屬性列表選擇或包含包含和排除鍵的對象。

[options.include []。attributes] - 要從子模型中選擇的屬性列表。

[options.include []。through.where] - 在連接模型上過濾belongsToMany關係。

[options.include []。through.attributes] - 從連接模型中爲belongsToMany關係選擇的屬性列表。

所以,[options.include[].through]只能在Belongs-To-Many關聯,而不是Belong-ToDiscoverySourceOrganization模型使用你的情況下使用。

相關問題