Luk*_*udi 5 javascript associations node.js sequelize.js
我是这个续集的新手。我尝试使用belongsToMany通过UserPermissions在用户和权限之间关联模型,这是我的代码。
-- 用户.js
const bcrypt = require('bcrypt');
const config = require('../config/general');
module.exports = (sequelize, DataTypes) => {
const User = sequelize.define('User', {
email: {
type: DataTypes.STRING,
allowNull: false,
unique: true,
validate: {
isLowercase: true,
notEmpty: true,
isEmail: true
}
},
username: {
type: DataTypes.STRING,
allowNull: false,
unique: true,
validate: {
isLowercase: true,
notEmpty: true,
min: 3
}
},
salt: {
type: DataTypes.STRING,
allowNull: true
},
password: {
type: DataTypes.STRING,
allowNull: false,
validate: {
notEmpty: true
}
},
}, {
underscored: true,
classMethods: {
associate: (models) => {
User.belongsToMany(models.Permission, {
through: 'UserPermissions',
foreignKey: 'user_id'
});
},
validPassword: (password, passwd, done) => {
const tmppass = password + config.secret;
bcrypt.compare(tmppass, passwd, (err, isMatch) => {
if (err) return done(err);
return done(null, isMatch);
});
}
}
});
User.beforeCreate( (user, option, done) => {
bcrypt.genSalt(SALT_WORK_FACTOR, (err, salt) => {
if (err) return done(err);
const tmppass = user.password + config.secret;
bcrypt.hash(tmppass, salt, (err, hash) => {
if (err) return done(err);
user.salt = salt;
user.password = hash;
return done(null, user);
});
});
});
return User;
};
Run Code Online (Sandbox Code Playgroud)
-- 权限.js
module.exports = (sequelize, DataTypes) => {
const Permission = sequelize.define('Permission', {
name: {
type: DataTypes.STRING,
allowNull: false,
validate: {
notEmpty: true
}
},
slug: {
type: DataTypes.STRING,
validate: {
isLowercase: true
}
},
description: {
type: DataTypes.TEXT
}
}, {
underscored: true,
classMethods: {
associate: (models) => {
Permission.belongsToMany(models.User, {
through: 'UserPermissions',
foreignKey: 'permission_id'
});
}
}
});
return Permission;
};
Run Code Online (Sandbox Code Playgroud)
根据这里关于belongsToMany 的续集文档,belongsToMany 将创建一个新模型,该模型链接到您加入的任何模型,对。
这将使用等效的外键 projectId 和 userId 创建一个名为 UserProject 的新模型。属性是否为驼峰式取决于表连接的两个模型(在本例中为 User 和 Project)。Sequelize Belongs-To-Many
但是当我尝试使用 迁移它时sequelize-cli,我没有看到任何已创建的连接表。该用户表中创建,该权限创建表,但UserPermissions不创建表。我错过了什么吗?还是我的代码有问题?
我正在使用postgres方言,"pg": "^6.4.0"并且"sequelize": "^4.3.1"
哦,是的,我真的很抱歉我的英语,我的英语不是很好。
回答你的问题(多年后):
文档可能会告诉您这样做以创建连接表:
在您的用户关联中:
User.belongsToMany(models.Permission, {
through: 'UserPermissions',
foreignKey: 'user_id'
});
Run Code Online (Sandbox Code Playgroud)
在您的权限关联中:
Permission.belongsToMany(models.User, {
through: 'UserPermissions',
foreignKey: 'permission_id'
});
Run Code Online (Sandbox Code Playgroud)
文档中未提及的过程部分是如何实际创建您在数据库中使用的这个表。要在数据库中创建这些函数使用的表,请使用以下代码创建迁移:
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('UserPermissions', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
permission_id: {
type: Sequelize.INTEGER,
references: {
model: 'Permissions',
key: 'id',
as: 'permission_id'
}
},
user_id: {
type: Sequelize.INTEGER,
references: {
model: 'Users',
key: 'id',
as: 'user_id'
}
},
createdAd: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
})
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('UserPermissions')
}
}
Run Code Online (Sandbox Code Playgroud)
使用 Sequelize CLI 将为您生成大部分代码。请注意,下面的model对象属性references是 postgres 中的表名(嗯,这对我有用)。
编辑:
另请注意,该through属性应该是一个模型,因此:
through: models.UserPermission
Run Code Online (Sandbox Code Playgroud)
并创建相关模型。
| 归档时间: |
|
| 查看次数: |
1805 次 |
| 最近记录: |