2016-03-21 36 views
0

1対多の関係を持つ2つのモデルを関連付ける問題が発生しています。なんらかの理由で、このクエリは関係を参照していてもエラーを投げています。ここで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 Associationエラーの未定義のプロパティ '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の複製です。

しかし、あなたが見えるようにクエリを書き直す必要があります。

[options.attributes] - あなたがしたい属性のリスト:Sequelizeのドキュメントによると

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

選択、または包含キーと除外キーを持つオブジェクトです。

[options.include []。attributes] - 子モデルから選択する属性のリストです。

[options.include []。through.where] - belongsToMany関係の結合モデルをフィルタリングします。

[options.include []。through.attributes] - belongsToMany関係の結合モデルから選択する属性のリスト。だから、

[options.include[].through]だけ DiscoverySourceOrganizationモデルのためにあなたが使用し Belongs-To-Many関連ではなく、 Belong-Toの場合に使用することができます。

関連する問題