mysql - Sequelize 将多个表关联到具有 2 个外键的 1 个表
问题描述
是否可以使用 2 个外键将多个表连接到单个表?我的桌子:
const Products = sequelize.define("Products", {name: sequelize.STRING});
const Categories = sequelize.define("Categories ", {name: sequelize.STRING});
const Translations = sequelize.define("Translations ", {name: sequelize.STRING});
我想用外键将 Translations 表连接到 Products 和 Categories
'object_id' (object_id = product_id or category_id) and 'object_type' ( object_type= 'products' or 'categories');
我针对的 mysql 查询:
SELECT *
FROM Translations t
JOIN Products p ON p.product_id = t.object_id AND t.object_type = 'products'
和
SELECT *
FROM Translations t
JOIN Categories c ON c.category_id = t.object_id AND t.object_type = 'categories'
同样在删除产品或类别中的行后从翻译中级联删除它;
解决方案
推荐阅读
- javascript - 在 Firebase 中验证 GET 请求(+ express 后端)?
- powershell - 跳过 Set-ADUser 脚本的空 CSV 单元格
- typescript - 如何引用您在 TS 中键入的对象的类型?
- java - spring security 一直重定向到它的默认登录页面
- c - 为什么递归的if条件失败后control不回传main函数?
- android - 为什么一个调用@Composable 函数的字体在默认配色方案中是绿色的,而另一个是蓝色的?
- html - 在 html 中捕获 iOS 设备扬声器的音频
- javascript - Fivem 脚本 - 在 5 秒后将 id 切换到头部设置为 false
- css - 在 :hover 上应用 CSS 样式到另一个元素
- c++ - QT Widget 应用程序在工作人员的 QTableView 更新期间冻结