我有一個USERS表,其中有兩個主鍵:id和mail,用戶與另一個表Contacts有1:1的關係。我想在RefreshToken表中將兩個外鍵「導出」爲郵件和ID。在Sequelize + Nodejs中的多個foreignkey
USERS表定義:
module.exports = function (sequelize, DataTypes) {
const Users = sequelize.define('Users', {
id: {
type: DataTypes.INTEGER(11),
autoIncrement: true,
primaryKey: true
},
firstname: {
type: DataTypes.STRING,
allowNull: true
},
lastname: {
type: DataTypes.STRING,
allowNull: true
},
email: {
type: DataTypes.STRING,
allowNull: false,
primaryKey: true
},
password: {
type: DataTypes.STRING,
allowNull: false
}
}, {
classMethods: {
associate: function (models) {
Users.hasOne(models.RefreshToken, {foreignKey:'userId'}
}
},
tableName: 'Users',
hooks: {
beforeCreate: user => {
const salt = bcrypt.genSaltSync();
user.password = bcrypt.hashSync(user.password, salt);
}
}
});
RefreshToken表定義:
module.exports = function (sequelize, DataTypes) {
const RefreshToken = sequelize.define('RefreshToken', {
idRefreshToken: {
type: DataTypes.INTEGER(11),
autoIncrement: true,
primaryKey: true
},
token: {
type: DataTypes.TEXT,
allowNull: true
},
expire: {
type: DataTypes.DATE,
allowNull: true
}
}, {
tableName: 'RefreshToken'
});
我讓我的問題更完整! – tinytanks