2016-11-09 9 views
2

Sequelizeは私の外部キーのアンダースコアを削除します。この中アンダースコアが削除されました

Role.belongsToMany(User, { foreignKey: 'user_id', through: UserRole });

結果:

Unknown column 'UserRole.UserId' in 'field list'

列がUSER_ID、およびいないユーザーIDと命名されているため。

underscore: trueオプションが設定されていても、それを行います。

これを解決する方法はありますか、それは私が間違っているか続行しているかわからないので、ナットを運転しています。

UserRole.js:

module.exports = { 
    attributes: { 
    roleId: { 
     type: Sequelize.INTEGER(11), 
     field: 'role_id', 
     primaryKey: true 
    }, 
    userId: { 
     type: Sequelize.INTEGER(11), 
     field: 'user_id', 
     primaryKey: true 
    } 
    }, 
    associations: function() { 
     UserRole.belongsTo(User, { foreignKey: 'user_id' }); 
     UserRole.hasOne(Role, { foreignKey: 'id' }); 
    }, 
    options: { 
    tableName: 'user_roles', 
    timestamps: false, 
    classMethods: {}, 
    instanceMethods: {}, 
    hooks: {} 
    } 
} 

Role.js

module.exports = { 
    attributes: { 
    id: { 
     type: Sequelize.INTEGER(11), 
     primaryKey: true 
    }, 
    name: { 
     type: Sequelize.STRING(50), 
    }, 
    description: { 
     type: Sequelize.STRING(50), 
    }, 
    }, 
    associations: function() { 
    Role.belongsToMany(User, { foreignKey: 'user_id', through: UserRole }); 
    }, 
    options: { 
    tableName: 'roles', 
    timestamps: false, 
    classMethods: {}, 
    instanceMethods: {}, 
    hooks: {} 
    } 
} 
+1

「アンダースコア」オプションをオンにしてみましたか?私はここにあなたのコードでそれが表示されないと重要です。 – tadman

+0

@tadman私はおそらく眠るべきです、Userモデルに 'underscored'オプションを追加するのを忘れました。どうもありがとう! – Joery

+0

私たちの最高のことが起こります。あなたがそれを得てうれしい! – tadman

答えて

2

あなたのモデルでunderscoreオプションを有効にしているようにそれは見えません。設定した場合、Sequelizeは命名規則の優先順位を尊重し、期待どおりのものをリンクします。

私は、アンダースコアの名前、長い時間のRailsの開発者、および大文字と小文字の混在した列名が壊れているように思えます。

関連する問題