作者:朱美萍客户 | 来源:互联网 | 2022-12-09 19:53
我的postgresql数据库中有2个模型,并使用sequelize和node:
用户
交易
并且像这样关联:
UserModel.hasMany(TransactionModel, { as: 'sentTransactions', foreignKey: 'senderId' });
UserModel.hasMany(TransactionModel, { as: 'receivedTransactions', foreignKey: 'receiverId' });
UserModel.belongsToMany(TransactionModel, { as: 'transactionLikes', through: 'UserLike', foreignKey: 'userId' });
TransactionModel.belongsTo(UserModel, { as: 'receiver' });
TransactionModel.belongsTo(UserModel, { as: 'sender' });
TransactionModel.belongsToMany(UserModel, { as: 'likers', through: 'UserLike', foreignKey: 'transactionId' });
这意味着用户有许多收到和发送的交易,每个用户可以"喜欢"许多交易.
如何删除事务并删除所有关联(接收者,发件人,liker)?我也不想删除用户.
我还想更新这样定义的用户模型,以便添加"email"属性:
const UserModel = db.define('user', {
id: { type: Sequelize.STRING, unique: true, primaryKey: true },
firstName: { type: Sequelize.STRING },
lastName: { type: Sequelize.STRING },
username: {
type: Sequelize.STRING,
unique: {
args: true,
msg: USERNAME_IS_TAKEN,
},
}
我该如何更新模型?现有实例会发生什么?
预先感谢您的帮助!