首页 > 解决方案 > 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'

同样在删除产品或类别中的行后从翻译中级联删除它;

标签: mysqlnode.jsormsequelize.jsassociations

解决方案


推荐阅读