gpt4 book ai didi

orm - ORM 续集可以具有多态性吗?

转载 作者:行者123 更新时间:2023-12-05 00:54:51 25 4
gpt4 key购买 nike

我有一个名为 User 的模型。我可以从 User 模型扩展另一个模型 Admin/mod 吗?我找到了续集文档,但我没有找到

最佳答案

是的,查看 Associations documentation ,或更完整地在 Scopes documentation .

从文档:

this.Comment = this.sequelize.define('comment', {
title: Sequelize.STRING,
commentable: Sequelize.STRING,
commentable_id: Sequelize.INTEGER
});

this.Comment.prototype.getItem = function() {
return this['get' + this.get('commentable').substr(0, 1).toUpperCase() + this.get('commentable').substr(1)]();
};

this.Post.hasMany(this.Comment, {
foreignKey: 'commentable_id',
constraints: false,
scope: {
commentable: 'post'
}
});
this.Comment.belongsTo(this.Post, {
foreignKey: 'commentable_id',
constraints: false,
as: 'post'
});

this.Image.hasMany(this.Comment, {
foreignKey: 'commentable_id',
constraints: false,
scope: {
commentable: 'image'
}
});
this.Comment.belongsTo(this.Image, {
foreignKey: 'commentable_id',
constraints: false,
as: 'image'
});

关于orm - ORM 续集可以具有多态性吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39114553/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com