sql - Knex/反对关系“一对多”
问题描述
我有一个具有类别关系的产品。每个产品可以有多个类别。
问题:我需要设计一个关系以包含产品具有“所有类别”关系的情况。我可以将关系添加到所有类别,但如果类别列表将被更新,“所有类别”将被损坏。产品:
id | name
1 | product1
2 | product2
3 | product3
产品类别:
id | product_id | category_id
1 | 1 | 1
1 | 1 | 2
1 | 2 | 2
4 | 3 | 3
类别:
id | name
1 | one
2 | two
3 | three
异议积关系映射:
static relationMappings = {
categories: {
relation: Base.ManyToManyRelation,
modelClass: path.resolve(__dirname, 'Category'),
join: {
from: 'products.id',
through: {
from: 'product_categories.product_id',
to: 'product_categories.category_id',
},
to: 'categories.id',
},
},
解决方案
推荐阅读
- python - 如何在 pandas 数据透视表中创建一个仅匹配填充列值的新列?
- java - 在spring boot swagger中手动指定POST请求体
- nginx - nginx - 禁用特定 URL 的 HTTPS 重定向
- c# - 什么是 NTLM/身份验证/协商 Web 身份验证
- django-cms - 显示页面的高级设置时出现 Django CMS 错误
- reactjs - React-Router-Dom 哈希路由器
- excel - 如何将此 Excel 宏保存为 .xls (Excel 97-2003) 格式,而不是 .xlsx?
- clang - Clang 标志的完整文档在哪里?
- javascript - 如何将此三元运算符表达式转换为 if/else 块?
- c - 对于 C,Visual Studio-2017 如何调试读取文本文件并在该文本文件之后打印输出的代码?