javascript - 如何在 Objection.js 中为不同类别的喜欢实现继承?
问题描述
我尝试使用 Objection.js 实现数据库设计,但我不知道如何定义具有多个互斥类别的继承关系。
有问题的案例如下:我想对帖子、评论和回复实现点赞。据我所知,“最可扩展的解决方案是只有一个“基本”表,并从中“继承”所有其他表。如另一个SO问题中所述。这个想法是将喜欢链接到一个实体,并且一个实体可以是帖子、评论或另一个实体。
User
id PK int
name string
Entity
id PK int
Like
userId int Fk >- User.id
entityId int Fk >- Entity.id
Article
id PK int Fk >- Entity.id
author int FK >- User.id
title string
text string
Comment
id PK int Fk >- Entity.id
articleId int FK >- Article.id
authorId int FK >- User.id
text string
--There can be more entities like reply, image, etc
在 Objection.js 中,您实现如下关系:
class Entity extends Model {
static tableName = 'entities';
static relationMappings = {
Post: {
relation: Model.HasOneRelation,
modelClass: Like,
join: {
from: 'post.id',
to: 'like.postId'
}
}
}
}
但是,如果这样做我不知道如何定义它也可以是评论或回复。不仅是邮政。
有没有办法存档这个?如果没有,我应该使用另一个 JS 库吗?我应该更改我的架构以使其与 Objection.js 一起使用吗?
相关链接:
解决方案
推荐阅读
- angular - 在 VSCode 中运行的 Angular 调试会话中未命中断点
- python - 如何使用用户输入在 Django 中命名对象?
- node.js - (节点:14000)UnhandledPromiseRejectionWarning:DiscordAPIError:无法发送空消息
- loops - 如何在 SPSS 上创建以下变量?
- python - 使用键盘库如何在 .is_pressed 中使用组合键?
- javascript - 状态更新未反映在自定义挂钩中
- ethereum - 将 nonReentrant 修饰符与应付函数一起使用会导致错误吗?
- javascript - 使用 Axios 使用 HTTP Put 上传文件,而不包含在请求正文中
- scrapy - Scrapy css 选择器找不到结果 - 但浏览器可以
- sql - 如何在单独的框中查询国家/地区列表?