使用PostgreSQL,我有一个表格应该与自身建立多对多的关系。这个表格叫做
是否有更好的方法?这种方法的问题在于每当我想要查找所有的集成时,都需要检查
例如,在Rails中将集成定义为关系不太容易,因为Rails期望有一个且仅有一个外键匹配。有更好的方法吗?这感觉很不优雅。我不介意使用PostgreSQL。
tools
,我使用一个名为integrations
的辅助表来实现多对多关系,它包含两个字段:tool_a_id
和tool_b_id
。是否有更好的方法?这种方法的问题在于每当我想要查找所有的集成时,都需要检查
tool_a
和tool_b
两个字段,例如:SELECT * FROM integrations
WHERE tool_a_id = x AND tool_b_id = x
当我想选择所有与另一个工具集成的工具时,我需要执行以下操作:
SELECT "tools".* FROM tools, integrations
WHERE ((tools.id = integrations.tool_a_id AND integrations.tool_b_id = x)
OR (tools.id = integrations.tool_b_id AND integrations.tool_a_id = x))
例如,在Rails中将集成定义为关系不太容易,因为Rails期望有一个且仅有一个外键匹配。有更好的方法吗?这感觉很不优雅。我不介意使用PostgreSQL。