2014-11-08 94 views
0

我有一個模型UserUserRole。目前這兩個角色中有兩個角色和許多用戶。我無法找到角色取數據用戶bookshelfjs節點關係

我看到的錯誤是:Error: ER_BAD_FIELD_ERROR: Unknown column 'user_roles.user_id' in 'where clause'

​​

var UserRole = bookshelf.Model.extend({ 
    tableName: 'user_roles', 
    users: function() { 
    return this.belongsToMany(User, 'users', 'role_id', 'id'); 
    } 
}); 

module.exports = UserRole; 

的knex遷移如下所示:

knex.schema.createTable('user_roles', function (table) { 
    table.increments(); 
    table.string('role').notNullable(); 
    table.timestamps(); 
}).table('users', function(table) { 
    table.integer('role_id').unsigned().references('user_roles.id'); 
}) 

任何想法?

回答

0

我想通了。該User模型是不正確的,應改爲:

role: function() { 
    return this.belongsTo(UserRole, 'id'); 
} 

使用user.role()現在作爲與變化的預期。

相關問題